[Java] Spring @Value does not evaluate but xml configuration works

in my xml I have:

<bean
    id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:myprop.properties</value>
        </list>
    </property>
</bean>

<context:annotation-config>

^ The above stays the same in both scenario

I have a simple class

public class Test {
    private String myProperty;

    // Getter / Setter

    @PostConstruct
    public void post(){
        System.out.println("Just Checking");
    }
}

Scenario 1: If I use @Value annotation, the myProperty value is always null:

@Component
public class Test {
    @Value("${prop.key}")
    private String myProperty;

    // Getter / Setter


    @PostConstruct
    public void post(){
        System.out.println("Just Checking");
    }
}

Scenario 2: However, if I do it in the xml, it will load properly

<bean id="test" class="com.mycompany.config.Test">
    <property
            name="myProperty"
            value="${prop.key}" />
</bean>

What am I missing?

Posted Aug 17, 2017, 7:43 PM UTC

13
Loading comments…