spring mvc,在controller中获取属性文件的值

参考链接:
http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-and-spring-servlet-xml-in-spring

http://stackoverflow.com/questions/10102216/why-is-spring-value-incompatible-with-controller

http://stackoverflow.com/questions/2055660/how-can-i-inject-a-property-value-into-an-annotation-configured-spring-mvc-3-0-c

关键是applicationContext.xml和 and spring-servlet.xml中定义的bean是在不同的container中存在的,彼此不能直接访问。
The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.

The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).


controller需要在定义controller的xml中声明所用的prperties文件:

<context:property-placeholder location="/WEB-INF/classes/*.properties" />


然后在controller中用@Vlaue读取

@Value("${some.property}")	
private String oneProperty;

注意,有点诡异的是oneProperty会直接被赋予some.property的值,不需要写set方法!

猜你喜欢

转载自javavsnet.iteye.com/blog/1717712