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 ?