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.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…