I am new to Spring and trying to inject a string with a value using the @Value("${loginpage.message}") annotation inside of a controller annotated with the @Controller annotation and the value of my string is being evaluated as the string "${loginpage.message}" and not what is inside my properties file.
Below is my controller with the string ‘message’ that I want to inject.
@Controller
public class LoginController extends BaseController {
@Value("${loginpage.message}")
private String message;
@RequestMapping("/")
public String goToLoginPage(Model model) {
model.addAttribute("message", message);
return "/login";
}
}
My application context looks like this:
<context:property-placeholder location="classpath:properties/application.properties" />
<context:annotation-config />
<context:component-scan base-package="com.me.application" />
My properties file has the line:
loginpage.message=this is a test message
Spring must be picking up the value at some point because whenever I change @Value("${loginpage.message}") to a value not in the properties file like @Value("${notInPropertiesFile}"), I get an exception.
It seems that the question has been already asked Spring 3.0.5 doesn't evaluate @Value annotation from properties
The difference between web app root and servlet application contexts is one of the top sources of confusion in Spring, see Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
From
@Valuejavadoc :From Spring documentation: