Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Value annotation not working

Tags:

java

spring

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);
like image 862
AfterWorkGuinness Avatar asked Mar 21 '26 17:03

AfterWorkGuinness


2 Answers

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.

like image 86
Sotirios Delimanolis Avatar answered Mar 24 '26 07:03

Sotirios Delimanolis


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.

like image 21
Artem Bilan Avatar answered Mar 24 '26 06:03

Artem Bilan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!