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文件都給引用進去了,就不會出現值取不到的情況了。

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