• 新鲜速递:Spring Boot3多模块项目跨包自动注入的方法,快速编写自己的starter项目


    问题背景

    现在我们有一个项目demo,有一个Jar包能力工具项目是commons。我们在开发commons的时候定义的包路径是com.commons,而demo项目则是com.demo,两个项目包路径不一样。所以在默认配置的情况下,demo是无法在启动的时候跨包路径扫描com.commons。因而用@Resource试图将commons里的组件注入到demo的组件里会在启动的时候报错。
    我们期待的效果是将com.commons项目引入到com.demo项目里直接就能用,像spring boot提供的那些starter一样。
    原因:在Spring Boot3中,传统的spring.factories不生效,所以自己写一个Starter的方法和Spring Boot 2.7以前的时代是不一样的。

    如何做

    这次我们不讲原理,直接上干货!玩的就是真实!
    对于commons项目只需要做以下三步即可:

    1、确保pom.xml里声明的打包类型是jar
    2、写一个Configuration类
    3、在资源目录下建立目录META-INF/spring/,并放入org.springframework.boot.autoconfigure.AutoConfiguration.imports文件(没错,扩展名就是imports),然后把第二步写的Configuration类路径放进去即可。

    做完以上三步,让demo项目依赖commons项目即可直接注入commons里的组件。

    第一步就不用说什么了,大家都会,第二步写的Configuration(配置类),给大家贴一下代码模板:

    package com.commons.spring;
    
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    // 下面这行把包路径换成你自己的
    @ComponentScan(basePackages = "com.commons")
    @SpringBootConfiguration
    public class BeanConfigScanConfig{
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    第三步建立目录和文件也不用说了,直接贴上文件内容,很简单:

    com.commons.spring.BeanConfigScanConfig
    
    • 1

    使用效果

    最后使用的时候假设commons有个类:

    package com.commons.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class TestService {
        public String sayHello(String name){
            return "你好,"+name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    然后到demo项目里直接用即可

    package com.demo.controller;
    
    import jakarta.annotation.Resource;
    import com.commons.service.TestService;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/")
    public class DemoController {
        @Resource
        TestService testService;
        @RequestMapping("/")
        public String demo(String name){
            return testService.sayHello(name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    扩展:如何让咱的commons项目接管生命周期,真正成为starter项目

    package com.commons.starter;
    
    import jakarta.annotation.Resource;
    import org.springframework.boot.context.event.ApplicationStartedEvent;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    
    @Component
    @Configuration
    public class CommonsStarter implements ApplicationListener {
    
        @Resource
        ApplicationContext applicationContext;
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            if(event instanceof ApplicationStartedEvent) {
            	// 这里编写你启动时要初始化的代码
            }
            // 还可以接管其他的生命周期:详细可以搜百度,这里不做赘述
            // ApplicationContextInitializedEvent
    		// ApplicationEnvironmentPreparedEvent
    		// ApplicationFailedEvent
    		// ApplicationPreparedEvent
    		// ApplicationReadyEvent
    		// ApplicationStartedEvent
    		// ApplicationStartingEvent
    		// EventPublishingRunListener
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    这样就给框架开发留下很大的想象空间了,比如初始化的时候自动连接数据源、自动建立定时任务、关闭的时候自动清理外部缓存等等,这一切对使用者来说只是引个包而已——方便、优雅。

  • 相关阅读:
    计算机体系结构:MIPS计算例题(1.7)
    手拉手springboot整合kafka发送消息
    Netty之I/O模型比对
    训练人工智能机器人的软实力
    Flink 侧输出流(SideOutput)
    Java面试题
    【元宇宙欧米说】众神时代——游戏与区块链结合,重构游戏产业新面貌
    无人机飞行控制系统技术,四旋翼无人机控制系统建模技术详解
    超高荧光量子产率近红外金纳米团簇
    CVPR 2022数据集汇总|包含目标检测、多模态等方向
  • 原文地址:https://blog.csdn.net/yry0304/article/details/133981537