user1745356
user1745356

Reputation: 4693

Spring: Value from a properties file is not autowired

I am using Spring 4. My configuration is:

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:contexts/root/spring-root-context.xml
        classpath:contexts/security/security-context.xml</param-value>
</context-param>

spring-root-context.xml

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

security-context.xml

<beans:bean id="myCustomFilter" class="filters.MyCustomFilter">
    <beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

credentials.properties

ClientSecret=qwerty

MyCustomFilter.java

public class MyCustomFilter extends AbstractAuthenticationProcessingFilter {

    @Value("${ClientSecret}")
    private String clientSecret;

}

The properties file is loaded

INFO RMI TCP Connection(3)-127.0.0.1 support.PropertySourcesPlaceholderConfigurer:172 - Loading properties file from class path resource [credentials.properties]

The value of clientSecret is not injected.

Upvotes: 0

Views: 844

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121560

Looks like you don't have <context:annotation-config> in your spring-root-context.xml.

From other side, if you configure MyCustomFilter as a <bean> just use it's property:

<beans:bean id="myCustomFilter" class="filters.MyCustomFilter">
    <beans:property name="authenticationManager" ref="authenticationManager" />
    <beans:property name="clientSecret" value="${ClientSecret}" />
</beans:bean>

If you have setter for that, of course.

Upvotes: 1

Related Questions