1

I currently load my properties file like so in spring

<context:property-placeholder location="test-esb-project-config.properties"/>
<context:property-placeholder location="esb-project-config.properties"/>

This seems to work great for properties use inside the xml files. How do I load a property from inside my java code now though? OR how do I inject some kind of Bundle or Config object so I don't have to inject 10 properties in one bean?

thanks, Dean

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • 1
    Do u mean this question here http://stackoverflow.com/questions/317687/inject-property-value-into-spring-bean ? – sunny Mar 16 '12 at 20:53

2 Answers2

1

using annotations @Value(${property}) worked much better and it injected the property into my bean without all the work of xml typing and adding a setter...way too much work going that route.

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
0

You can either have setters for each property and wire them with the property reference.

public class MyBean{
    public void setFoo(String foo){ /* etc */}
    public void setBar(String bar){ /* etc */}
}

<bean class="foo.bar.MyBean">
    <property name="foo" value="${my.properties.foo}" />
    <property name="bar" value="${my.properties.bar}" />
</bean>

Or you can inject a Properties Object into your Spring Bean.

public class MyBean{
    public void setProperties(Properties properties){
        // init your properties here
    }
}

<bean class="foo.bar.MyBean">
    <property name="properties" value="classpath:/path.to.properties" />
</bean>

Both of these would also work without XML when using the @Value annotation.
(see Expression Language > Annotation-based Configuration)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588