Spring-3 ApplicationContext

Spring容器的最基本接口就是BeanFactory,它有一个子接口:ApplicationContext,称Spring上下文。

BeanFactory包含以下5个方法: 

- boolean       containsBean(String name):         Spring容器中是否包含id为name的bean 

- < T>T          getBean(Class<> requied Type):  该类型时,对象唯一可以获取 

- Object         getBean(String name)

 - <T> T         getBean(String name, Class requiedType)

 - Class<?>   getType(String name)

ApplicationContext包含BeanFactory的全部功能,还有额外的功能,通常不会使用BeanFactory,建议使用ApplicationContext

ApplicationContext的额外功能:

一、默认初始化suoyou所有singleton Bean,也可以通过配置取消。

        当系统创建ApplicationContext时,默认会预初始化所有的singleton Bean。包括调用构造器创建实例,及根据<property>执行setter方法,意味着系统前期开销大。

bean:

package codeEETest;
public class Person
{
	public Person()
	{
		System.out.println("==正在执行Person无参数的构造器==");
	}
	public void setTest(String name)
	{
		System.out.println("正在调用setName()方法,传入参数为:" + name);
	}
}
xml:

<bean id="chinese" class="codeEETest.Person">
	<!-- 驱动Spring执行chinese Bean的setTest()方法,以"孙悟空"为传入参数 -->
	<property name="test" value="孙悟空"/>
</bean>
即使主程序只有一行代码,即创建容器:
ApplicationContext ctx = new ClassPathXmlApplicationContext("codeEETest/beans.xml");

Spring容器仍然会初始化Bean,结果:

==正在执行Person无参数的构造器==
正在调用setName()方法,传入参数为:孙悟空


为了防止初始化,配置lazy-init="true"

	<bean id="chinese" class="codeEETest.Person" lazy-init="true">
		<!-- 驱动Spring执行chinese Bean的setTest()方法,以"孙悟空"为传入参数 -->
		<property name="test" value="孙悟空"/>
	</bean>

二、ApplicationContext事件机制



<think>我们正在解决SSM项目中出现的`BeanDefinitionStoreException`,原因是`applicationContext.xml`文件找不到(`FileNotFoundException`)。根据引用内容,这个问题通常与配置文件路径设置有关。以下是逐步分析和解决方案:###1.**检查web.xml中的contextConfigLocation配置**在web.xml中,Spring通过`contextConfigLocation`参数指定配置文件的位置。常见的错误包括:-路径错误(如拼写错误)-文件名不匹配-使用了错误的协议(如`classpath:`vs`classpath*:`)根据引用[1]和引用[2]:```xml<context-param><param-name>contextConfigLocation</param-name><!--正确示例:确保路径和文件名正确--><param-value>classpath:applicationContext.xml</param-value></context-param>```如果配置文件在子目录中(例如`spring/`目录下),则需要指定子目录路径:```xml<param-value>classpath:spring/applicationContext.xml</param-value>```或者使用通配符加载多个配置文件(注意:通配符的使用需要确保文件存在且路径正确):```xml<param-value>classpath*:spring/applicationContext*.xml</param-value>```**注意**:`classpath:`表示从类路径(通常是`src/main/resources`)的根目录开始查找,而`classpath*:`会搜索所有类路径(包括jar文件)。###2.**确认配置文件的实际位置**-在Maven项目中,Spring的配置文件通常放在`src/main/resources`目录下(或子目录中)。-检查`applicationContext.xml`是否在正确的目录中。例如:-如果web.xml中配置为`classpath:applicationContext.xml`,则文件必须直接位于`src/main/resources`下。-如果配置为`classpath:spring/applicationContext.xml`,则文件应在`src/main/resources/spring/`目录下。###3.**检查Maven资源过滤配置**引用[4]提到,如果使用了Maven,可能需要配置`pom.xml`以确保资源文件(包括XML)被正确复制到`target/classes`(类路径)中:```xml<build><resources><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include></includes><filtering>true</filtering></resource></resources></build>```如果配置文件放在`src/main/java`下的某个包中(通常不推荐,但有时用于MyBatis的MapperXML),则需要额外配置:```xml<resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource>```###4.**检查IDEA的项目结构设置**在IntelliJIDEA中:-确保`resources`目录被标记为`ResourcesRoot`(右键目录>MarkDirectoryas>ResourcesRoot)。-清理并重新构建项目(Build>RebuildProject)。###5.**运行时类路径检查**部署后,检查生成的`target`目录(或WAR文件):-确保`applicationContext.xml`被复制到`target/classes`(或子目录)中。-对于Web项目,检查WAR文件中的`WEB-INF/classes`目录。###总结步骤1.**核对web.xml中的`contextConfigLocation`**:确保路径和文件名与项目中的实际位置完全一致。2.**检查文件物理位置**:确认配置文件在`src/main/resources`的预期路径下。3.**检查Maven配置**:确保资源文件被正确包含。4.**IDE设置**:标记资源目录并重新构建。如果以上步骤仍不能解决问题,请提供更多信息(如项目结构截图、web.xml完整配置等)。§§相关问题§§1.如何在Maven项目中正确配置资源文件?2.Spring的`classpath:`和`classpath*:`有什么区别?3.在IntelliJIDEA中如何标记资源目录?4.如何解决Spring配置文件加载顺序问题?</think>### SSM项目中`applicationContext.xml`文件找不到的解决方案 #### 问题分析 根据错误信息`FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist`和引用内容,核心问题是Spring容器无法定位配置文件。常见原因包括: 1. **路径配置错误**:web.xml中`contextConfigLocation`参数值不匹配实际路径(引用[1][2]) 2. **文件位置错误**:配置文件未放在资源目录(如`src/main/resources`) 3. **构建工具问题**:Maven未正确打包资源文件(引用[4]) 4. **命名不一致**:实际文件名与配置不符(如`spring-beans.xml` vs `applicationContext.xml`) --- ### 逐步解决方案 #### 1. **检查web.xml配置** ```xml <!-- 修正前(错误示例) --> <param-value>classpath:spring-beans.xml</param-value> <!-- 修正后(正确配置) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> ``` **关键点**: - 确保`<param-value>`中的文件名与物理文件名**完全一致**(包括大小写) - 使用`classpath:`前缀表示从资源根目录查找 - 若文件在子目录中,需指定路径:`classpath:config/applicationContext.xml` > 引用[2]案例显示拼写错误导致相同异常:`contextConfigLocation`误写为`contextConfigLocation`[^2] #### 2. **验证文件物理位置** - **标准Maven项目结构**: ``` src/main/resources/applicationContext.xml // 直接存放 src/main/resources/spring/applicationContext.xml // 子目录存放 ``` - **检查步骤**: 1. 在IDEA中右键点击文件 → "Reveal in File Explorer" 2. 确认文件位于`target/classes`目录(编译后路径) #### 3. **配置Maven资源过滤** 在`pom.xml`中添加(引用[4]): ```xml <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <!-- 包含所有XML文件 --> </includes> </resource> <!-- 若XML文件在java目录 --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> ``` 执行Maven命令:`mvn clean package` 重新构建 #### 4. **检查文件命名一致性** - 确认实际文件名与配置匹配: - `applicationContext.xml`(Spring主配置) - `spring-mvc.xml`(Spring MVC配置) - `spring-mybatis.xml`(MyBatis整合配置)[^3] #### 5. **使用绝对路径测试(临时方案)** ```xml <param-value>file:D:/project/src/main/resources/applicationContext.xml</param-value> ``` > 仅用于验证文件是否存在,正式环境需改回`classpath:` --- ### 排查流程图 ```mermaid graph TD A[报错 FileNotFoundException] --> B{检查web.xml配置} B -->|正确| C{检查target目录是否存在文件} B -->|错误| D[修正contextConfigLocation值] C -->|存在| E[检查Maven资源过滤配置] C -->|不存在| F[移动文件到resources目录] E --> G[执行mvn clean package] F --> G G --> H[重启服务器验证] ``` ### 典型错误案例 1. **路径拼写错误** `classpath:applicationContext.xml` → `classpath:applicaionContext.xml`(漏写&#39;t&#39;) 2. **未使用资源过滤** XML文件放在`src/main/java`但未在pom.xml配置资源包含[^4] 3. **多模块项目路径问题** 子模块中需使用:`classpath*:applicationContext.xml`(带星号表示跨模块搜索) > 引用[1][2]均显示路径配置是主要故障点[^1][^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值