Announcement Announcement Module
Collapse
No announcement yet.
Annotations config: simple String properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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="[email protected]" />
    </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="[email protected]" />
    </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, 09:53 PM.

  • #2
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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

        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.

        Comment


        • #5
          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.

          Comment

          Working...
          X