Results 1 to 5 of 5

Thread: Annotations config: simple String properties

  1. #1
    Join Date
    Apr 2006
    Location
    Montreal, Canada
    Posts
    178

    Question Annotations config: simple String properties

    Hi all,

    I'm migrating my Spring configuration from XML bean definitions to class annotations. It's going well (and I like the result) but there's one issue for which I can't find a satisfying answer which involves bean property values.

    Suppose I have the following bean definition:

    Code:
    <bean id="mailBuilder" class="com.xyz.MailBuilder">
        <property name="fromAddress" value="admin@xyz.com" />
    </bean>
    This is clean and it allows me to change the fromAddress property without rebuilding the entire application. But if MailBuilder is annotated with @Component, how can I inject a simple String property into fromAddress?

    The best I could come up with so far is to create the following bean definition:

    Code:
    <bean id="fromAddress" class="java.lang.String">
        <constructor-arg index="0" value="admin@xyz.com" />
    </bean>
    and use the @Resource annotation on the MailBuilder's setter. It works, but it seems awfully verbose for just a simple String property. Plus, if I have 2 builders (one for user activation, the other for lost passwords), I also have to "namespace" these properties to distinguish between them.

    Any ideas on how to improve this?

    Thanks a bunch!
    Last edited by spiff; Mar 5th, 2008 at 08:53 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    It is an issue I ran into also. What we did on a project is instead of using plain Strings in the MailBuilder we used a properties object, placed all the properties in a property file. Loaded that and have the properties object be autowired.

    Drawback is that you will have to retrieve the property from the Properties yourself, advantage is that you can quite easily change the contents of the properties file.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2006
    Location
    Montreal, Canada
    Posts
    178

    Default

    Good idea, thanks! In the end I've preferred to create a property map using the <util:map> element, then autowired the map to the MailBuilder object. This way all configuration elements are kept inside the applicationContext.xml file, which was one of the motivations for putting the String properties there in the first place.

  4. #4

    Default

    Hi I am looking to achieve similar ends in my project, can you provide some more details for this or any brief code snippets or point me to some doc link.
    Any pointers will be much appreciated,
    thanks,
    robin

    Quote Originally Posted by Marten Deinum View Post
    It is an issue I ran into also. What we did on a project is instead of using plain Strings in the MailBuilder we used a properties object, placed all the properties in a property file. Loaded that and have the properties object be autowired.

    Drawback is that you will have to retrieve the property from the Properties yourself, advantage is that you can quite easily change the contents of the properties file.

  5. #5
    Join Date
    Sep 2007
    Location
    London,UK
    Posts
    21

    Default

    I've created a project which allow you to inject properties like components into your Spring objects for Spring 2.5.*:

    http://code.google.com/p/spring-property-annotations/

    For Spring 3 you can use the @Value("${propery.key}") annotation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •