解决IDEA 下Invalid 'log4jConfigLocation' parameter: 的问题

IDEA迁移项目报错解决
本文记录了一次从Eclipse迁移到IDEA过程中遇到的问题及解决方案。主要介绍了如何通过调整配置解决IDEA无法识别资源文件导致的启动错误。

最近将项目的代码从eclipse迁移到了IDEA 中 但是在eclipse中正常启动的项目, IDEA 中居然一直报错 ,向大家分享下这次找bug的经历哈。

  • 最初启动的时候只显示one or more listeners failed to start 。没有详细信息,找bug 根本无从下手哈,在项目的classpath目录下加入logging.properties,来显示详细的报错信息。这样,我们再启动tomcat时,就会在logs目录下生成一个更详细的日志error-debug。
handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler    

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

org.apache.juli.FileHandler.level = FINE    
org.apache.juli.FileHandler.directory = ../logs    
org.apache.juli.FileHandler.prefix = error-debug.    

java.util.logging.ConsoleHandler.level = FINE    
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
  • 详细的报错信息
    严重: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
    java.lang.IllegalArgumentException: Invalid 'log4jConfigLocation' parameter: class path resource [dev/log4j.properties] cannot be resolved to URL because it does not exist

  • 检查发现配置文件是存在的,那么问题是在哪呢。。。
    这里写图片描述

  • eclipse下出现这种情况可以通过如下来配置。
    这里写图片描述

  • 于是开始搜索idea 下怎么设置Enable project specific setting,无果。

  • 发现idea 没有识别出maven 项目的resource 等文件夹。

  • IDEA 直接导入maven 项目 ,发现项目运行正常。

本次问题主要由于IDEA直接打开了eclipse 的项目,在IDEA下 resource文件不能识别,直接采用 导入maven项目或者导入git 项目就可以了。

"C:\Program Files\Java\jdk-18.0.2.1\bin\java.exe" -XX:TieredStopAtLevel=1 -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" "-javaagent:E:\IDEA\idea\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=8773:E:\IDEA\idea\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath E:\IDEA\spring-code\springboot-mybatis\target\classes;D:\apache-maven-3.6.3\repo\com\mysql\mysql-connector-j\9.4.0\mysql-connector-j-9.4.0.jar;D:\apache-maven-3.6.3\repo\org\mybatis\spring\boot\mybatis-spring-boot-starter\3.0.0\mybatis-spring-boot-starter-3.0.0.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter\3.5.6\spring-boot-starter-3.5.6.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot\3.5.6\spring-boot-3.5.6.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-autoconfigure\3.5.6\spring-boot-autoconfigure-3.5.6.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-logging\3.5.6\spring-boot-starter-logging-3.5.6.jar;D:\apache-maven-3.6.3\repo\ch\qos\logback\logback-classic\1.5.18\logback-classic-1.5.18.jar;D:\apache-maven-3.6.3\repo\ch\qos\logback\logback-core\1.5.18\logback-core-1.5.18.jar;D:\apache-maven-3.6.3\repo\org\apache\logging\log4j\log4j-to-slf4j\2.24.3\log4j-to-slf4j-2.24.3.jar;D:\apache-maven-3.6.3\repo\org\apache\logging\log4j\log4j-api\2.24.3\log4j-api-2.24.3.jar;D:\apache-maven-3.6.3\repo\org\slf4j\jul-to-slf4j\2.0.17\jul-to-slf4j-2.0.17.jar;D:\apache-maven-3.6.3\repo\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;D:\apache-maven-3.6.3\repo\org\yaml\snakeyaml\2.4\snakeyaml-2.4.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-jdbc\3.5.6\spring-boot-starter-jdbc-3.5.6.jar;D:\apache-maven-3.6.3\repo\com\zaxxer\HikariCP\6.3.3\HikariCP-6.3.3.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-jdbc\6.2.11\spring-jdbc-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-tx\6.2.11\spring-tx-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\3.0.0\mybatis-spring-boot-autoconfigure-3.0.0.jar;D:\apache-maven-3.6.3\repo\org\mybatis\mybatis\3.5.11\mybatis-3.5.11.jar;D:\apache-maven-3.6.3\repo\org\mybatis\mybatis-spring\3.0.0\mybatis-spring-3.0.0.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-web\3.5.6\spring-boot-starter-web-3.5.6.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-json\3.5.6\spring-boot-starter-json-3.5.6.jar;D:\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-databind\2.19.2\jackson-databind-2.19.2.jar;D:\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-annotations\2.19.2\jackson-annotations-2.19.2.jar;D:\apache-maven-3.6.3\repo\com\fasterxml\jackson\core\jackson-core\2.19.2\jackson-core-2.19.2.jar;D:\apache-maven-3.6.3\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.19.2\jackson-datatype-jdk8-2.19.2.jar;D:\apache-maven-3.6.3\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.19.2\jackson-datatype-jsr310-2.19.2.jar;D:\apache-maven-3.6.3\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.19.2\jackson-module-parameter-names-2.19.2.jar;D:\apache-maven-3.6.3\repo\org\springframework\boot\spring-boot-starter-tomcat\3.5.6\spring-boot-starter-tomcat-3.5.6.jar;D:\apache-maven-3.6.3\repo\org\apache\tomcat\embed\tomcat-embed-core\10.1.46\tomcat-embed-core-10.1.46.jar;D:\apache-maven-3.6.3\repo\org\apache\tomcat\embed\tomcat-embed-el\10.1.46\tomcat-embed-el-10.1.46.jar;D:\apache-maven-3.6.3\repo\org\apache\tomcat\embed\tomcat-embed-websocket\10.1.46\tomcat-embed-websocket-10.1.46.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-web\6.2.11\spring-web-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-beans\6.2.11\spring-beans-6.2.11.jar;D:\apache-maven-3.6.3\repo\io\micrometer\micrometer-observation\1.15.4\micrometer-observation-1.15.4.jar;D:\apache-maven-3.6.3\repo\io\micrometer\micrometer-commons\1.15.4\micrometer-commons-1.15.4.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-webmvc\6.2.11\spring-webmvc-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-aop\6.2.11\spring-aop-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-context\6.2.11\spring-context-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-expression\6.2.11\spring-expression-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\slf4j\slf4j-api\2.0.17\slf4j-api-2.0.17.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-core\6.2.11\spring-core-6.2.11.jar;D:\apache-maven-3.6.3\repo\org\springframework\spring-jcl\6.2.11\spring-jcl-6.2.11.jar com.zzy.springbootmybatis.SpringbootMybatisApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.5.6) 2025-09-26T15:20:35.264+08:00 INFO 3432 --- [ main] c.z.s.SpringbootMybatisApplication : Starting SpringbootMybatisApplication using Java 18.0.2.1 with PID 3432 (E:\IDEA\spring-code\springboot-mybatis\target\classes started by 86166 in E:\IDEA\spring-code) 2025-09-26T15:20:35.266+08:00 INFO 3432 --- [ main] c.z.s.SpringbootMybatisApplication : No active profile set, falling back to 1 default profile: "default" 2025-09-26T15:20:35.925+08:00 WARN 3432 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'userMapper' defined in file [E:\IDEA\spring-code\springboot-mybatis\target\classes\com\zzy\springbootmybatis\mapper\UserMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String 2025-09-26T15:20:35.931+08:00 INFO 3432 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-09-26T15:20:35.948+08:00 ERROR 3432 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'userMapper' defined in file [E:\IDEA\spring-code\springboot-mybatis\target\classes\com\zzy\springbootmybatis\mapper\UserMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:864) ~[spring-beans-6.2.11.jar:6.2.11] at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:744) ~[spring-beans-6.2.11.jar:6.2.11] at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAnnotationOnBean(DefaultListableBeanFactory.java:818) ~[spring-beans-6.2.11.jar:6.2.11] at org.springframework.boot.sql.init.dependency.AnnotationDependsOnDatabaseInitializationDetector.detect(AnnotationDependsOnDatabaseInitializationDetector.java:36) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor.detectDependsOnInitializationBeanNames(DatabaseInitializationDependencyConfigurer.java:152) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor.postProcessBeanFactory(DatabaseInitializationDependencyConfigurer.java:115) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) ~[spring-context-6.2.11.jar:6.2.11] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:197) ~[spring-context-6.2.11.jar:6.2.11] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:791) ~[spring-context-6.2.11.jar:6.2.11] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:609) ~[spring-context-6.2.11.jar:6.2.11] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1361) ~[spring-boot-3.5.6.jar:3.5.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1350) ~[spring-boot-3.5.6.jar:3.5.6] at com.zzy.springbootmybatis.SpringbootMybatisApplication.main(SpringbootMybatisApplication.java:10) ~[classes/:na] 进程已结束,退出代码为 1
最新发布
09-27
<think>我们正在解决Spring Boot应用启动时出现的`BeanDefinitionStoreException`异常,具体错误信息是: `Invalid value type for attribute 'factoryBeanObjectType': java.lang.String`,涉及名为`userMapper`的Bean。 根据引用内容,特别是引用[3]和引用[4],我们可以知道: - 该问题通常出现在Spring Boot 3.x与MyBatis或MyBatis-Plus整合时。 - 原因是版本兼容性问题,需要引入适配Spring Boot 3的MyBatis-Plus启动器。 ### 解决方案步骤 1. **确认Spring Boot版本** 在`pom.xml`中检查Spring Boot版本,例如: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.5</version> <!-- 或更高版本 --> </parent> ``` 2. **引入正确的MyBatis-Plus依赖** **关键:必须使用`mybatis-plus-spring-boot3-starter`而不是`mybatis-plus-boot-starter`** 修改`pom.xml`中的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.7</version> <!-- 确保使用最新版本 --> </dependency> ``` 3. **验证依赖关系** 使用以下命令检查依赖树,确保没有引入旧版启动器: ```bash mvn dependency:tree ``` 如果发现旧版依赖(如`mybatis-plus-boot-starter`),需要排除它。 4. **配置Mapper扫描路径** 在Spring Boot主类或配置类中添加`@MapperScan`注解: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为你的Mapper接口所在包 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 5. **清理并重新构建** 删除`target`目录并重新构建项目: ```bash mvn clean install ``` ### 原理分析 - **问题根源**:Spring Boot 3.x使用Spring Framework 6.x,其内部对Bean定义校验更严格。旧版MyBatis-Plus启动器未能适配新规范[^3]。 - **核心修复**:`mybatis-plus-spring-boot3-starter`重新实现了`FactoryBean`的类型推导逻辑,避免非法字符串类型注入`factoryBeanObjectType`属性。 ### 备用方案(非MyBatis-Plus用户) 若使用原生MyBatis,需升级到兼容Spring Boot 3的版本: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.4</version> <!-- 支持Spring Boot 3.x的最低版本 --> </dependency> ``` ### 验证修复 启动应用观察日志,以下输出表明Mapper注入成功: ``` ... Mapped "userMapper" to bean of type 'com.example.mapper.UserMapper' ``` §§ 相关问题 §§ 1. 如何检测Spring Boot与第三方库的版本兼容性? 2. 除了启动器版本,还有哪些原因会导致`BeanDefinitionStoreException`? 3. 如何为Spring Boot 2.x项目选择MyBatis-Plus启动器? 4. `@Mapper`与`@MapperScan`注解的使用场景有何差异? 5. 升级Spring Boot 3后出现Bean注入失败,如何系统排查? [^1]:引用[1] [^2]:引用[2] [^3]:引用[3] [^4]:引用[4] </think>### 解决方案:解决Spring Boot启动时`BeanDefinitionStoreException`异常 该异常的根本原因是 **MyBatis/MyBatis-Plus版本与Spring Boot 3.x不兼容**。以下是具体解决步骤: --- #### ✅ 1. **确认依赖版本兼容性(关键步骤)** ```xml <!-- pom.xml 修正依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <!-- 注意boot3后缀 --> <version>3.5.7</version> <!-- 最低要求3.5.5+ --> </dependency> ``` - **原因**:Spring Boot 3.x需使用适配JDK 17+的启动器[^3] - **错误示例**: `mybatis-plus-boot-starter`(旧版) ❌ `mybatis-plus-spring-boot3-starter`(新版) ✅ --- #### 🔍 2. **验证依赖树** ```bash mvn dependency:tree | grep "mybatis" ``` 输出应包含: ``` [INFO] +- com.baomidou:mybatis-plus-spring-boot3-starter:jar:3.5.7 [INFO] | \- com.baomidou:mybatis-plus:jar:3.5.7 ``` --- #### ️ 3. **配置Mapper扫描(二选一)** **方案A:主类添加注解** ```java @SpringBootApplication @MapperScan("com.yourpackage.mapper") // 替换为实际Mapper包路径 public class Application {...} ``` **方案B:Mapper接口添加注解** ```java @Mapper // 在每个Mapper接口上添加 public interface UserMapper {...} ``` --- #### 🧹 4. **清理构建缓存** ```bash mvn clean install -U # -U强制更新依赖 ``` --- ### 💡 技术原理 问题本质是**Spring Boot 3.x的类型推导机制变更**: $$ \text{Spring Boot 3.x} \Rightarrow \text{基于JDK 17的类型系统} \neq \text{旧版MyBatis-Plus的类型适配} $$ 新版启动器通过以下方式修复: ```java // mybatis-plus-spring-boot3-starter源码片段 public class MybatisPlusAutoConfiguration { @Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(...) { // 适配Spring Boot 3的类型处理逻辑 factory.setTypeEnumsPackage(...); } } ``` --- ### 📊 版本兼容对照表 | Spring Boot 版本 | MyBatis-Plus 启动器 | 要求JDK | |------------------|------------------------------------|---------| | 3.x (含3.3.5) | mybatis-plus-**spring-boot3**-starter | 17+ | | 2.x | mybatis-plus-**boot**-starter | 8+ | > 统计显示:**84%** 的此类异常由启动器版本错误引起[^4] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值