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;
Spring由于web配置导致的spring配置文件找不到的问题的解决方案的更多相关文章
- Spring mvc web 配置
Spring Framework本身没有Web功能, Spring MVC使用WebApplicationContext类扩展ApplicationContext ,使得拥有web功能.那么,Spri ...
- shiro + maven 的web配置(不整合spring)
本文采用的是1.4.0版本的shiro 官方中说的1.2之前,和之后的shiro配置分别为: 1.2之前: <filter> <filter-name>iniShiroFilt ...
- spring的Java配置入门(Spring Boot学习笔记之一)
spring的Java配置 1.创建maven项目 使用idea创建maven项目,这里顺便提一下,idea真的比eclipse好用,早点熟悉吧.然后就是maven是java项目管理最主流的工具,自己 ...
- Spring与Web框架(例如Spring MVC)漫谈——关于Spring对于多个Web框架的支持
在看Spring MVC的官方文档时,最后一章是关于Spring对于其它Web框架的支持(如JSF,Apache Struts 2.x,Tapestry 5.x),当然Spring自己的MVC框架Sp ...
- Spring Mvc Web 配置拦截规则与访问静态资源 (三)
拦截规则配置 1. *.do <!-- Processes application requests --> <servlet> <servlet-name>app ...
- Spring与web MVC的整合——Spring的应用上下文管理
问题1 如何让web容器加载你的web MVC框架 对于基于servlet的web容器来说,遵循的是servlet规范,入口配置文件是web.xml.这类web容器会在启动的时候会而且仅会加载如下三种 ...
- ssm中web配置各框架的配置文件路径方式
一.在web文件中配置 使用逗号隔开 二.在applicationContext.xml文件中配置或引用 以下是引用方式 注: <import />标签要放在所有bean配置的最前面.
- spring的基本配置
一:web.xml (1)spring mvc的配置 <servlet> <description>spring mvc servlet</description> ...
- Spring Boot 属性配置和使用
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
随机推荐
- JoinMap
#include <iostream> #include <fstream> using namespace std; #include <opencv2/core/co ...
- delphi 拆分字符串
最近在使用Delphi开发一种应用系统的集成开发环境.其中需要实现一个字符串拆分功能,方法基本原型应该是:procedure SplitString(src: string ; ch: Char; v ...
- Android-XML与JSON的理解-JSON的数据格式
据我了解,在多年以前浏览器客户端和服务器它们的通讯数据交互格式是XML, 使用XML来规定数据格式可读性确实非常强,XML的魅力确实很大,也很成熟,但是也有不足之处,就是在网络传输的时候对流量要求特别 ...
- Android Camera的使用(一) 读书笔记
原文地址 https://blog.csdn.net/junzia/article/details/52301199 拍照步骤1.添加权限2.开启相机时check一下是否有摄像头3.对预览大小.照片大 ...
- MySQL简单实现多字段模糊查询
我所做的商城项目前些时提了新需求,要求前台搜索商品除了能通过商品名称搜索到以外,还可以通过别个信息搜索,比如:商品编号.详情内容描述等等,类似于全文搜索了.我首先想到的就是lucene,但是对代码这样 ...
- HTML5使用总结(一)
自己在“上海某985大学”待了五年,有蛮多的不舍.但是终究还是要离开.下面对这几年HTML5的使用做一个总结.总结是一种技术的沉淀.HTML5大家现在很火,它的标准已经出来.在标准还没有成型的时候,相 ...
- 自定义TFS工作项“所有链接”列表中的列
这个功能只有使用团队资源管理器查看工作项才有
- [Erlang31]Erlang trace总结
在一个并行的世界里面,我们很难做到单步断点调试来定位问题(太多的消息飞来飞去),Erlang设计者也深刻体会到这一点,推出了另一个trace机制. 通过这个trace,你可以: .特定进程集内的函数调 ...
- 命令行web客户端与HTTP REST API调试工具
1.命令行web客户端 curl wget httpie 2.优雅的REST API调试工具 insomnia postman
- mysql5.7 创建新表时提示时间戳非法
# 背景 mysql版本5.7.8,需要创建新表,研发提供的sql文件,执行后报错如下: ERROR (): Invalid default value for 'deleted_at' 就猜测到时因 ...