【沉浸式解决问题】微服务子模块引入公共模块的依赖后无法bean未注入

一、问题描述

在微服务项目中的公共模块进行了Mybatis Plus配置,创建了配置类并添加了@Configuration注解,其他模块引入该模块后不生效
我这里是在Mybatis Plus公共模块中注入了自定义SQL方法,测试模块获取失败

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

在这里插入图片描述


二、场景还原

前情提要:【抽丝剥茧知识讲解】【沉浸式解决问题】自定义MyBatis-Plus 3.5.12中的BaseMapper,并实现真正的批量插入
在文章的最后单独创建了一个模块配置Mybatis-plus,在一个模块内测试没问题,在其他模块测试时就获取不到了


三、原因分析

spring boot启动需要一个启动类,上面有@SpringBootApplication注解,但是该启动类启动时只扫描自己目录下的各种bean,引入的依赖中的正常是不自动装载的


四、解决方案

给公共模块中的bean设置成自动配置就好了

在公共模块的resources目录下创建固定目录
在这里插入图片描述

输入目录名META-INF后回车

META-INF

在这里插入图片描述
继续在META-INF目录下创建spring,要分开创建

spring

在这里插入图片描述
再创建文件
在这里插入图片描述

输入文件名org.springframework.boot.autoconfigure.AutoConfiguration.imports后回车

org.springframework.boot.autoconfigure.AutoConfiguration.imports

在这里插入图片描述
右键配置类复制类路径
在这里插入图片描述
粘贴到org.springframework.boot.autoconfigure.AutoConfiguration.imports中,如果有其他的需要自动配置的bean粘贴在下面即可
在这里插入图片描述
再次运行就注入成功了

@SpringBootTest
public class UserTest {
    @Autowired
    UserMapper userMapper;

    @Autowired
    IUserService userService;

    void truncate() {
        userMapper.truncateTable(); // 每次测试前情况表
    }

    List<User> userList() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 300000; i++) {
            User user = new User();
            user.setName("姓名"+i);
            userList.add(user);
        }
        return userList;
    }

    @Test
    void test3() {
        truncate();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        userService.saveBatch(userList());
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeMillis());
    }

在这里插入图片描述

五、拓展知识

SpringBoot提供了SPI扩展的方式引入其他依赖中的Bean,即自动装配,SpringBoot2.7以前可以通过配置META-INF/spring.factories文件,
SpringBoot2.7以后可以通过配置META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,不过SpringBoot2.7以后为了兼容以前的版本习惯,依然可以配置META-INF/spring.factories

参考文献

SpringBoot自动配置原理:@EnableAutoConfiguration解析


喜欢的点个关注吧><!祝你永无bug!

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝吻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值