Gelu Bugnaru
Gelu Bugnaru

Reputation: 33

Cannot get value from properties using @Value in Spring

I have a project template using spring and I need values from properties file, but when I run the program the value is null. Here is my code: Properties file contains: TOWN=Cluj

    public class BaseTest {

           @Value("${TOWN}")
           public String stringValue;

           @BeforeTest
           public void beforeTest(){
               ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
               Student student =  context.getBean("studentBean", Student.class);
               student.displayName();
               student.displayTown();

               System.out.println(stringValue); // -> this is null
           }
       }

Beans file :

        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

            <context:property-placeholder location="classpath:stable.properties"/>
            <context:component-scan base-package="com.base" />

            <bean name="studentBean" class="com.model.Student">
                <property name="name" value="MyName"/>
                <property name="town" value="${TOWN}"/>
            </bean>

        </beans>
        public class Student {
            private String name;
            private String town;

            public String getName(){
                return name;
            }

            public void setName(String name){
                this.name = name;
            }

            public String getTown() {
                return town;
            }

            public void setTown(String town) {
                this.town = town;
            }

            public void displayName(){
                System.out.println("Name: " + name);
            }

            public void displayTown(){
                System.out.println("Town: " + town);
            }

        }

In BaseTest when displayTown() method is called the value from properties file works, but if a try to use the value as a variable in BaseTest the values is null. Could you help me please ?

Upvotes: 1

Views: 154

Answers (1)

MyTwoCents
MyTwoCents

Reputation: 7624

As you have confirmed BaseTest is just a normal class. Values wont be injected automatically.

In xml

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

It automatically configures PropertyPlaceholderConfigurer, which replaces the ${} placeholders, which are resolved against a specified properties file.

<context:component-scan base-package="com.base" />

Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, @Controller, @RestController, @ControllerAdvice, and @Configuration stereotypes will be detected.

<bean name="studentBean" class="com.model.Student">
    <property name="name" value="MyName"/>
    <property name="town" value="${TOWN}"/>
</bean>

For Student class name and town fields will be automatically picked from property file

Please note this will happen only for Student class.

What you are trying is to get value in BaseTest class which is just a class.

Either Define a bean to inject property for BaseTest class like this

<bean name="baseTestBean" class="classpath.BaseTest">
    <property name="stringValue" value="${TOWN}"/>
</bean>

you wont need @Value("${TOWN}") in class

or

add any one configuration and it will automatically scan your class provided its under com.base package

Upvotes: 1

Related Questions