read this article on SO,并有一些澄清问题。创建和访问属性文件在春天mvc
我把我的config.properties在src /主/资源
在spring-servlet.xml config file
我增加了以下内容:
<context:property-placeholder location="classpath:config.properties"/>
在我的业务层,我想通过访问它
@Value("${upload.file.path}")
private String uploadFilePath;
Eclipse显示错误:
The attribute value is undefined for the annotation type Value
我可以不访问业务层中的属性还是属性文件只能在控制器中读取?
UPDATE :: 的src /主/爪哇/ com.companyname.controllers/homecontroller.java
public String home(Locale locale, Model model) {
MyServiceObject myObj = new MyServiceObject();
System.out.println("Property from my service object: = " + myObj.PropertyValue());
if(myObj.PerformService())
{
///
}
}
的src /主/爪哇/ com.companyname.services/MyService.java
public class MyServiceObject {
@Value("${db.server.ip}")
private String _dbServerIP;
public String PropertyValue() {
return _dbServerIPaseURL;
}
}
Another site where I found the explanation
如果不是在控制器中,相应的上下文配置文件位于何处? – user1361914
在控制器java文件中更改导入是否解决了问题? Spring MVC中,你通常有IoC容器和至少两个配置文件http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s/11709272#11709272 –
如果它是一个dao或一个服务(而不是控制器),那么它将成为根Web应用程序上下文配置文件,我相信。当然,假设DAO在根web应用上下文中被实例化。 –