Spring root WebApplicationContext会启动两次

本文记录了一位开发者在使用springspringmvchibernate框架进行项目开发时遇到的SpringrootWebApplicationContext启动两次的问题。通过调整项目的WebProjectSettings的Contextroot设置,成功解决了这一困扰,避免了不必要的资源消耗。

最近遇到一个很奇怪的问题在这里记录一下

spring springmvc hibernate 项目

在 eclipse 下开发。
Spring root WebApplicationContext会启动两次

Initializing Spring root WebApplicationContext
......
Initializing Spring FrameworkServlet 'dispatcherServlet'
......



Initializing Spring root WebApplicationContext
......
Initializing Spring FrameworkServlet 'dispatcherServlet'
......

一直以为是配置文件的问题,在spring和springmvc 扫描包的时候出的问题,但是改了好几天 一直没有效果,

但是 原本的项目 名后来被改过,不过没注意过什么时候开始,变成启动两次了,
但是
项目的Web Project Settings 的Context root 是原先的项目名,改成后面的项目后,就不会启动两次了,不知道是什么原因,在这里记录一下。

代码报错六月 10, 2025 9:51:26 上午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-bio-8080"] 六月 10, 2025 9:51:26 上午 org.apache.catalina.core.StandardService startInternal 信息: Starting service Tomcat 六月 10, 2025 9:51:26 上午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/7.0.47 六月 10, 2025 9:51:28 上午 org.apache.catalina.core.ApplicationContext log 信息: No Spring WebApplicationInitializer types detected on classpath 六月 10, 2025 9:51:28 上午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring root WebApplicationContext log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader). log4j:WARN Please initialize the log4j system properly. 六月 10, 2025 9:51:31 上午 org.apache.catalina.core.StandardContext listenerStart 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookService': Unsatisfied dependency expressed through field 'bookMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookMapper' defined in file [D:\ideaworkspace\BookManagementSystem2\target\classes\com\example\book\dao\BookMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\ideaworkspace\BookManagementSystem2\target\classes\mapper\BookMapper.xml]'; nested exception is java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.example.book.dao.
最新发布
06-11
### 关于Spring项目中sqlSessionFactory和BookMapper依赖注入错误及Mapped Statements冲突问题的解决方案 在Spring项目中,当遇到`UnsatisfiedDependencyException`异常时,通常是因为`sqlSessionFactory`或`BookMapper`的依赖注入未正确配置[^1]。此外,`Mapped Statements collection already contains value for xxxx`错误通常是由于MyBatis配置文件中的SQL映射重复定义导致的[^3]。 以下是解决这些问题的具体方法: #### 1. 检查`sqlSessionFactory`配置 确保`SqlSessionFactoryBean`的配置正确无误。以下是一个典型的`SqlSessionFactoryBean`配置示例: ```xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mappers/*.xml"/> </bean> ``` - 确保`configLocation`指向正确的MyBatis配置文件。 - 确保`mapperLocations`路径与实际的Mapper XML文件路径一致。 - 如果使用了包扫描,则避免同时指定`mapperLocations`和包扫描,否则可能导致重复加载[^1]。 #### 2. 避免Mapper重复加载 如果同时使用了`<mappers>`标签和`@MapperScan`注解,则可能会导致Mapper被多加载,从而引发`Mapped Statements collection already contains value for xxxx`错误。例如: ```xml <mappers> <package name="com.example.mapper"/> </mappers> ``` ```java @MapperScan(basePackages = "com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 上述两种方式会重复加载Mapper文件,因此需要选择其中一种方式进行配置[^3]。 #### 3. 解决Mapper方法重名问题 如果Mapper接口中存在与MyBatis框架自带方法重名的情况(如`save`、`delete`等),也可能导致冲突。可以通过以下方式解决: - 修改自定义方法名称以避免与框架方法重名。 - 使用`@Select`、`@Insert`等注解明确指定SQL语句,避免依赖XML文件中的默认命名规则。 #### 4. 检查依赖注入 对于`BookMapper`的依赖注入问题,确保其已被正确扫描并注册为Spring Bean。例如: ```java @Repository public interface BookMapper extends BaseMapper<Book> { // 自定义方法 } ``` 在Spring Boot项目中,可以通过`@MapperScan`注解指定Mapper接口所在的包: ```java @MapperScan("com.example.mapper") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 如果仍然出现`UnsatisfiedDependencyException`,可以尝试手动注入`BookMapper`实例: ```java @Autowired private SqlSessionTemplate sqlSessionTemplate; @Bean public BookMapper bookMapper() { return sqlSessionTemplate.getMapper(BookMapper.class); } ``` #### 5. 调试与日志分析 启用MyBatis的日志功能,帮助定位问题。可以在`application.properties`中添加以下配置: ```properties logging.level.org.mybatis=DEBUG logging.level.com.example.mapper=DEBUG ``` 通过日志输出,检查Mapper文件是否被重复加载以及SQL语句是否正确解析[^4]。 --- ### 示例代码 以下是一个完整的Spring Boot项目配置示例: ```java // Spring Boot启动类 @MapperScan("com.example.mapper") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } // Mapper接口 @Repository public interface BookMapper extends BaseMapper<Book> { @Select("SELECT * FROM books WHERE id = #{id}") Book findById(Integer id); } // MyBatis配置类 @Configuration public class MyBatisConfig { @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mappers/*.xml")); return factoryBean.getObject(); } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值