Spring 3.0.5 does not evaluate @Value annotation from properties

Trying to automatically post properties to a bean in Spring 3.0.5.RELEASE, I use:

  • config.properties :

     username=myusername 
  • main-components.xml :

     <context:property-placeholder location="classpath:config.properties" /> 
  • MyClass:

     @Service public class MyClass { @Value("${username}") private String username; ... } 

As a result, the username is literally set to "${username}" , so the expression does not receive parsing. My other auto-connect relationship with this class is established, and Spring does not raise any exceptions. I also tried adding @Autowired , but that didn't help.

If I parse the properties for a single bean and then use @Autowired + @Qualifier , it works:

 <bean id="username" class="java.lang.String"> <constructor-arg value="${username}"/> </bean> 

Any ideas on how to use @Value only? Maybe I need to include some Spring dependency that I don't have? Thanks you

+26
spring properties frameworks autowired
Mar 11 2018-11-11T00:
source share
1 answer

Found a question. Copy / paste from comments:

Do you have <context:property-placeholder> in the same application context as the MyClass bean (not in the parent context)? - axtavt

You're right. I moved <context:property-placeholder> from the context defined by ContextLoaderListener to the servlet context. Now my values ​​are parsed. Thank you very much! - alex

+17
Mar 11 '11 at 16:55
source share



All Articles