Spring由於web配置導致的spring配置文件找不到的問題的解決方案


在把某項技術整合到Spring中的時候,我們時常會發現報如下錯誤:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field:

查閱了很多方法 都沒有實際的解決 后來才發現是因為spring的配置文件沒有放到

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

的 <context-param>中造成的:

具體有如下兩種解決方案:

1.把所有的XML配置文件放到<param-value>中,一般來說XML后面的英文逗號可加也可不加(一般系統能識別):

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
 
    <param-value>
    classpath:spring-dao.xml,
   classpath:spring-service.xml,
    classpath:springmvc-servlet.xml
    </param-value>

  </context-param>

  

2.用*來匹配有一定相同點的XML文件 並放到<param-value>中,這在一定程度上減少了代碼的冗余:

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    
    <param-value>classpath*:spring-*.xml</param-value>
    
  </context-param>

 原理介紹:

1, 在web.xml中定義 contextConfigLocation參數.spring會使用這個參數加載.所有逗號分割的xml.如果沒有這個參數,spring默認加載web-inf/applicationContext.xml文件.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml,
classpath*:conf/spring/applicationContext_staff*.xml,
classpath*:conf/spring/applicationContext_security.xml
classpath*:conf/spring/applicationContext_modules*.xml
classpath*:conf/spring/applicationContext_cti*.xml
classpath*:conf/spring/applicationContext_apm*.xml
</param-value>
</context-param> 

contextConfigLocation 參數定義了要裝入的 Spring 配置文件。原理說明如下:
、利用ServletContextListener 實現。
Spring 提供ServletContextListener 的一個實現類ContextLoaderListener ,該類可以作
為listener 使用,它會在創建時自動查找WEB-INF/ 下的applicationContext.xrnl 文件。因
此,如果只有一個配置文件,並且文件名為applicationContext.xml ,則只需在web.xml
文件中增加如下代碼即可:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有多個配置文件需要載入,則考慮使用<context-para即元素來確定配置文件的
文件名。由於ContextLoaderListener加載時,會查找名為contextConfigLocation的參數。
因此,配置context-param時參數名字應該是contextConfigLocation。
帶多個配置文件的web.xml 文件如下:
<1-- XML 文件的文件頭二〉
<?xml version="l.O" encoding="工80-8859-1"?>
< 1-- web.xm1 文件的DTD 等信息一〉
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems. 工口c.//DTD Web Application 2.3//EN"
''http://java.sun.com/dtd/web-app_2_3.dtd''>
<web-app>
<!一確定多個配置文件>
<context-param>
<1-- 參數名為contextConfigLocation…〉
<param-name>contextConfigLocation</param-name>
<!一多個配置文件之間以,隔開二〉
<param-value>/WEB-工NF/daoContext.xml./WEB-INF/application
Context.xml</param-value>
</context-param>
<!-- 采用listener創建Applicat工onContext 實例-->
<listener>
<listener-class>org.spr工ngframework.web.context.ContextLoader
Listener</listener-class>
</listener>
</web-app>
如果沒有contextConfigLocation 指定配置文件,則Spring 自動查找application
Context. xrnl 配置文件。如果有contextConfigLocation,則利用該參數確定的配置文件。
該參數指定的一個字符串, Spring 的ContextLoaderListener 負責將該字符串分解成多個
配置文件,逗號","、空格" "及分號";"都可作為字符串的分割符。
如果既沒有applicationContext. xrnl 文件,也沒有使用contextConfigLocation參數確
定配置文件,或者contextConfigLocation確定的配置文件不存在。都將導致Spring 無法
加載配置文件或無法正常創建ApplicationContext 實例

配置一個spring為加載而設置的servlet可以達到同樣效果.
采用load-on-startup Servlet 實現。
Spring 提供了一個特殊的Servllet 類: ContextLoaderServlet。該Servlet 在啟動時,會
自動查找WEB-IN日下的applicationContext. xml 文件。
當然,為了讓ContextLoaderServlet 隨應用啟動而啟動,應將此Servlet 配置成
load-on-startup 的Servleto load-on-startup 的值小一點比較合適,因為要保證Application
Context 優先創建。如果只有一個配置文件,並且文件名為applicationContext. xml ,則在
web.xml 文件中增加如下代碼即可:
<servlet>
<servlet-name>context</servlet口-arne>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</
servlet-class>
<load-on-startup>l</load-o 口-startup>
</servlet>

。帶多個配置文件的web且nl 文件如下:
<!-- XML 文件的文件頭-->
<?xml version="1.0" encoding="工SO-8859-1"?>
<! -- web.xml 文件的DTD 等信息→
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems , 工口c.//DTD Web Application 2.3//EN"
''http://java.sun.com/dtd/web-app_2_3.dtd''>
<web-app>
<'一確定多個配置文件一>
<context-param>
<!-- 參數名為contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<!-- 多個配置文件之間以,隔開一〉
<param-value>/WEB-工NF/daoContext.xml, !WEB-工NF/applicationContext.
xml</param-value>
</context-param>
<!一采用load-on-startup Servlet 創建Applicat工onContext 實例一〉
<servlet>
<servlet-narne>context</servlet-narne>
<servlet-class>org.springframework.web.context.ContextLoader
Servlet</servlet-class>
<!一下面值小一點比較合適,會優先加載一〉
<load-on-startup>l</load-on-startup>
</servlet>
</web-app>


2, 使用匹配符

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

比 如說用到Hibernate,則把hibernate相關的配置放在applicationContext-hibernate.xml這一個文件,而一 些全局相關的信息則放在applicationContext.xml,其他的配置類似.這樣就可以加載了,不必寫用空格或是逗號分開! 


3, 如果使用struts加載多個spring配置文件.下面這個配置的其實也是contextConfigLocation變量.

struts-config.xml里面加這個
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" 
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml,,,,,,,"/>

4,如果是非j2ee應用直接程序加載.

ApplicationContext act = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});

BeanDefinitionRegistry reg = new DefaultListableBeanFactory();

XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);

reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));

reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));

BeanFactory bf = (BeanFactory)reg;


免責聲明!

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



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