3

Properties annotated with @Value are not being set on my beans.

My context and classes are inside a jar that is on the classpath of another project. Its spring context imports my spring context and my spring context loads a config file from the classpath and contains various bean definitions.

Lazy loading is not being used anywhere, my jar is using Spring 3.1.4 and the project using my jar is using Spring 3.2.3.

Logger showing properties file is loaded when the external project loads it's context (which imports mine)

[main] INFO  Loading properties file from class path resource [connector-config.properties] - (PropertyPlaceholderConfigurer.java:172:)

Excerpt from my Spring context:

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    lazy-init="false">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:connector-config.properties</value>
        </list>
    </property>
</bean>
...
    <!-- The actual beans referenced are not foo bar and com but should not be relevant to this issue -->
<bean id="requestGenerator" class="com.connector.RequestGenerator">
    <constructor-arg name="foo" ref="foo" />
    <constructor-arg name="bar" ref="bar" />
    <constructor-arg name="com" ref="com" />
</bean>

config file that is on classpath of project using my jar

ruleType=PAUL
response.poolSize=10
ack.poolSize=10
#This needs to be in minutes
max.run.time=100
base.dir=\\

Class from external project loading a bean from my context: By inspecting the requestGen object in Eclipse debug mode I can see the property ruleType is null. Given the above properties file, ruleType should be "PAUL" but it is null.

public class App
{
public static void main(Straing[] args)
{
    @SuppressWarnings("resource")
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
    RequestGenerator requestGen = context.getBean("requestGenerator", RequestGenerator.class);
7
  • will you post RequestGenerator ? what value you are expecting set Commented Mar 26, 2014 at 20:35
  • Posted the relevant part of RequestGenerator and the properties file Commented Mar 26, 2014 at 21:06
  • Show us the rest of the bean configuration, specifically the requestGenerator bean definition. Commented Mar 26, 2014 at 21:09
  • Nothing you've shown seems to indicate the behavior you've described. Show us where you see that the field ruleType is null. Do you get an NPE? Show us the stack trace? Commented Mar 26, 2014 at 21:14
  • My bad, let me fix up my post. Thanks for all the help so far. Commented Mar 26, 2014 at 21:15

2 Answers 2

9

The @Value annotation is processed by AutowiredAnnotationBeanPostProcessor which is typically registered if you have a <component-scan> or <annotation-config> configuration in XML (or directly with a bean definition). You need to add either of those, probably <annotation-config> since you've said you don't have any @Component classes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the problem was a missing <annotation-config>. Couldn't see the forest from the trees.
3

Since Spring 3.1 it isn't recommended to use PropertyPlaceholderConfigurer for such task. For more info see its JavaDoc

From other side, show, please, your connector-config.properties and the code of RequestGenerator to have more info about an issue.

1 Comment

This solution actually worked for me, not the accepted answer. In my case I had some older Spring configurations that defined PropertyPlaceholderConfigurer. Once I removed those references, everything worked as documented.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.