@Value annotation not resolved in a class that belongs to dependency jar

965 views Asked by At

I have a simple web-app that includes a dependency jar for some functionality and needs to use a property value defined in main app code.

Thanks in advance to help me out on this.


dependency.jar contains

public class MyClass {

    @Value("${abc.def}")
    private String abc;  // DOES NOT GET RESOLVED. IS ALWAYS NULL

}

dependency-context.xml

my-app.war contains

public class LocalClass {

    @Value("${abc.def}")
    private String abc;  // GETS RESOLVED TO CORRECT PROPERTY VALUE
}

context.xml imports dependency-context.xml

2

There are 2 answers

0
scottmf On

You may be running into this bug. It makes it very difficult to use the @Value with multiple PropertyPlaceholderConfigurers from different projects using import context. To solve this problem try to autowire the existing PropertyPlaceholderConfigurer into your bean configuration method (assuming you can use @Configuration) and update the object in the method with the values from your project.

0
Custard On

@scottmf is on the right lines, but you can't read properties directly from PropertyPlaceholderConfigurer :(

Instead, I created a String @Bean in the war...

<bean id="myProperty" class="java.lang.String">
    <constructor-arg value="ABC"/>
</bean>

which can be @Autowired into the jar class:

@Bean
public Object myBean(String myProperty) { ... }