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

  

spring註解@Value取不到值

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

 

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

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

項目中常常會用到配置文件,定義成properties的形式比較常見,爲了方便使用通常在spring配置文件中作以下配置:spring

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

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

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

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

<!-- 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配置.webapp

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

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

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

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

相關文章
相關標籤/搜索