spring结合disconf时@Value注解获取不到值

在controller中利用@Value注解获取配置信息时,发现没有获取到,网上看了很多文章,由于在项目中集成了disconf与网友提供的方案稍有出入。首先我们得知道,

在使用spring mvc时,实际上是两个spring容器:

1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件
org.springframework.web.servlet.DispatcherServlet
这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中

2,applicationContext.xml 是另外一个,也需要注入属性文件
org.springframework.web.context.ContextLoaderListener

在我的工程中contex.xml中把beans.xml中的配置都给引用进去了,但是在controller中我只用到了disconf.xml中配置的相关参数值,所以我只需要在我的工程中的springmvc-servlet.xml配置中加了这一句<import resource="classpath:/config/spring/spring-disconf.xml" /> 就把disconf.xml中配置的properties文件都给引用进去了,就不会出现值取不到的情况了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章