zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property报错

2023-04-18 13:16:59 时间

错误背景

笔者在建一个新的Spring cloud 项目时,出现的报错提示为:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property

错误原因

经过网上搜索后,主要说是controller和xml有重名方法,或者启动类有问题,如@SpringBootApplication()的括号里不能有东西。
检查后,锁定到是我在config类里已经写了@MapperScan()的注解:

@Configuration
@EnableTransactionManagement
@MapperScan("com.demo.service.mapper")
public class MyBatisPlusConfig {
}

解决方法

那么启动类里的@MapperScan()需要更改成@ComponentScan()。
启动类里修改为:

@SpringBootApplication
@ComponentScan("com.demo.service.mapper")
public class EduApplication {
    public static void main(String[] args) {
        SpringApplication.run(EduApplication.class, args);
    }
}

或者直接把config的@MapperScan删了,但感觉会失去配config的一些意义

修改后,即不再报错,但是项目还有点其他小问题

总结

@ComponentScan

组件扫描注解,用来扫描@Controller @Service @Repository这类,主要就是定义扫描的路径从中找出标志了需要装配的类到Spring容器中

@MapperScan

扫描mapper类的注解,就不用在每个mapper类上加@MapperScan了

注意

这两个注解是可以同时使用的,但是需要改为@MapperScan(basePackages = {})的形式。
或者只使用@MapperScan()去扫描mapper包,让项目启动自己去扫描swagger配置类的包。
同一个类扫描的是同一路径会产生错误。

还是有很多东西需要搞懂,spring底层以及相关特征还需要进一步了解。
请大家多多支持点赞关注!