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

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

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

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

关键是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方法!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值