java - How can I inject a property value into a Spring Bean which was configured using annotations?

ID : 10215

viewed : 42

Tags : javaspringdependency-injectionjava

Top 5 Answer for java - How can I inject a property value into a Spring Bean which was configured using annotations?

vote vote

100

You can do this in Spring 3 using EL support. Example:

@Value("#{systemProperties.databaseName}") public void setDatabaseName(String dbName) { ... }  @Value("#{strategyBean.databaseKeyGenerator}") public void setKeyGenerator(KeyGenerator kg) { ... } 

systemProperties is an implicit object and strategyBean is a bean name.

One more example, which works when you want to grab a property from a Properties object. It also shows that you can apply @Value to fields:

@Value("#{myProperties['github.oauth.clientId']}") private String githubOauthClientId; 

Here is a blog post I wrote about this for a little more info.

vote vote

88

Personally I love this new way in Spring 3.0 from the docs:

private @Value("${propertyName}") String propertyField; 

No getters or setters!

With the properties being loaded via the config:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"       p:location="classpath:propertyFile.properties" name="propertiesBean"/> 

To further my glee I can even control click on the EL expression in IntelliJ and it brings me to the property definition!

There's also the totally non xml version:

@PropertySource("classpath:propertyFile.properties") public class AppConfig {      @Bean     public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {         return new PropertySourcesPlaceholderConfigurer();     } 
vote vote

72

There is a new annotation @Value in Spring 3.0.0M3. @Value support not only #{...} expressions but ${...} placeholders as well

vote vote

61

<context:property-placeholder ... /> is the XML equivalent to the PropertyPlaceholderConfigurer.

Example: applicationContext.xml

<context:property-placeholder location="classpath:test.properties"/>   

Component class

 private @Value("${propertyName}") String propertyField; 
vote vote

55

Another alternative is to add the appProperties bean shown below:

<bean id="propertyConfigurer"      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">         <property name="location" value="/WEB-INF/app.properties" /> </bean>    <bean id="appProperties"            class="org.springframework.beans.factory.config.PropertiesFactoryBean">         <property name="singleton" value="true"/>          <property name="properties">                 <props>                         <prop key="results.max">${results.max}</prop>                 </props>         </property> </bean> 

When retrieved, this bean can be cast to a java.util.Properties which will contain a property named results.max whose value is read from app.properties. Again, this bean can be dependency injected (as an instance of java.util.Properties) into any class via the @Resource annotation.

Personally, I prefer this solution (to the other I proposed), as you can limit exactly which properties are exposed by appProperties, and don't need to read app.properties twice.

Top 3 video Explaining java - How can I inject a property value into a Spring Bean which was configured using annotations?

Related QUESTION?