spring注解@Value取不到值【轉】


  

spring注解@Value取不到值

今天在一個項目中發現一個情況,在Service中取不到name,{name}字符串,找了好久,最后在一篇文章中找到解決方案.

 

解決這個問題的一篇文章(轉)

@Value取不到值引出的spring的2種配置文件applicationContext.xml和xxx-servlet.xml

項目中經常會用到配置文件,定義成properties的形式比較常見,為了方便使用一般在spring配置文件中做如下配置:

<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />

這樣在程序代碼中直接用@Value(“${name}”)就能直接取到properties文件中定義的變量值.

但是在一個項目中發現一個情況,在Controller中取不到這個值,直接輸出了${name}字符串,並沒有解析出值,而在service中卻能取到.有點奇怪啊,明顯在Controller中貌似並沒有引入properties文件中的變量,而被當做普通的字符串處理了..突然想到這個項目有2個配置文件,1個在WEB-INF下的springmvc-servlet.xml,1個在classpath下的applicationContext.xml,其中applicationContext.xml中定義有placeholder.

說實話之前並沒有注意過這個配置文件的區別,一直以為只是放置的位置不一樣而已,借助這次機會吧,查詢到了一些資料.先看一則引入spring在web.xml中的配置:

<!-- springmvc配置開始 -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 可以自定義servlet.xml配置文件的位置和名稱,默認為WEB-INF目錄下,名稱為[<servlet-name>]-servlet.xml,如spring-servlet.xml
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    -->
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- springmvc配置結束 -->

<!-- Spring配置開始 --> 
<listener> 
   <listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<!-- 指定Spring Bean的配置文件所在目錄。默認配置在WEB-INF目錄下 --> 
<context-param> 
   <param-name>contextConfigLocation</param-name> 
   <param-value>classpath:config/applicationContext.xml</param-value> 
</context-param>

<!-- Spring配置結束 -->

可以看到分為spring配置和springmvc配置2種.其中spring配置以監聽器的形式引入,不指定xml配置文件地址則默認查找WEB-INF下的applicationContext.xml文件.springmvc則以 servlet形式引入,當沒有指定引入的xml配置文件地址時,則會自動引入WEB-INF下的[servlet-name]-servlet.xml文件,本例中為springmvc-servlet.xml.引入順序為先引入spring配置,再引入servlet形式的springmvc配置.

值得注意的幾點是:springmvc的配置文件中可以直接用id引入spring配置文件中定義的bean,但是反過來不可以.每一個springmvc的配置文件xxx-servlet.xml對應一個web.xml中的servlet定義.當存在多個springmvc配置文件時候,他們之間是不能互相訪問的.

在百度中別人的帖子中看到一段應該是官方的原文解釋,我摘抄過來並粗糙的直譯一下:

Spring lets you define multiple contexts in a parent-child hierarchy. 
spring允許你定義多個上下文在父子繼承關系中

The applicationContext.xml defines the beans for the “root webapp context”, i.e. the context associated with the webapp. 
applicationContext.xml文件是為了”根webapp應用上下文”定義bean, 也就是說它的上下文是和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, 
spring-servlet.xml文件(或是其他的你習慣的稱呼)是為了一個servlet應用上下文呢定義bean. 在一個webapp中可以有多個此配置文件,

one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2). 
每一個spring的servlelt(例如: 名為spring1的servlet擁有配置文件spring1-servlet.xml, 名為spring2的servlet擁有配置文件spring2-servlet.xml).

Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa. 
在spring-servlet.xml中定義的bean可以直接引用在applicationContext.xml中定義的bean, 但是反過來不可以.

All Spring MVC controllers must Go in the spring-servlet.xml context. 
所有springmvc的Controller必須在spring-servlet.xml對應的上下文中運行.

In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets 
在大多數簡單的情況下, applicationContext.xml對應的上下文並不必須. 它通常用來包含那些bean用來在webapp中所有servlet之間共享.

in a webapp. If you only have one servlet, then there’s not really much point, unless you have a specific use for it. 
如果你只有一個servlet, 那么實際沒有什么必要定義applicationContext.xml, 除非你有特別應用.

那么回到最開始的@Value取不到值的問題,現在的可以清楚由於Controller是定義在springmvc的servlet配置文件中的,查找,故只需要將placeholder重新在springmvc的配置中配置一遍,Controller中的@Value注解便能取到值了.

 

轉載:http://blog.csdn.net/jml1437710575/article/details/52020936


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM