飞道的博客

SpringBoot实践(三十八):自定义spring-boot-starter

440人阅读  评论(0)

目录

自动配置原理

 自定义starter包

导入springboot的自动配置依赖 

测试业务代码

spring.factories配置

​编辑

 本地包上传

使用自定义starter依赖

测试和配置


自动配置原理

基于springBoot的starter机制能够让我们在使用外部包时候非常方便,只需要引入该组件提供的starter包即可,比如在pom中引入mybatis的mybatis-plus-boot-starter依赖包,那么springboot启动时候会自动扫描所有的依赖该包下META-INF文件夹下的spring.factories把自动配置类加入容器:

在spring.factories中会指定自动配置类:


  
  1. # Auto Configure
  2. org.springframework.boot.env.EnvironmentPostProcessor=\
  3. com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
  4. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  5. com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
  6. com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

其中MybatisPlusAutoConfiguration.java中会初始化若干的配置,在MybatisPlusProperties.class文件中,还有其他一些关于jdbc连接的条件注解;


  
  1. @Configuration
  2. @ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
  3. @ConditionalOnSingleCandidate(DataSource.class)
  4. @EnableConfigurationProperties({MybatisPlusProperties.class})

 自定义starter包

测试自定义starter包,该starter包中引入springboot的自动配置依赖,配置spring.factories文件中的自动加载类,实现的功能是在测试的springBoot工程中引入自定义的starter包的pom依赖后,能够调用starter包中的某些实例方法,具体而言是读取application.properties或者application.yml的配置输出;

首先创建1个空的项目,作为自定义starter包和测试的springboot工程的父工程,添加2个子模块:

导入springboot的自动配置依赖 

创建spring初始化器生成的子模块test01-spring-boot-starter,跟mybatis类似,或者直接copy,增加spring-boot-starter的依赖管理(不引入),只引入1个自动配置依赖;


  
  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.boot </groupId>
  5. <artifactId>spring-boot-dependencies </artifactId>
  6. <version>2.3.7.RELEASE </version>
  7. <type>pom </type>
  8. <scope>import </scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot </groupId>
  15. <artifactId>spring-boot-autoconfigure </artifactId>
  16. <scope>compile </scope>
  17. </dependency>
  18. </dependencies>

测试业务代码

创建1个属性配置类TestProperties.java,读取application.properties中的配置:


  
  1. package com.example.hello.bean;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @ConfigurationProperties(prefix = "test.hello")
  6. public class TestProperties {
  7. String prefix;
  8. String suffix;
  9. public String getPrefix () {
  10. return prefix;
  11. }
  12. public void setPrefix (String prefix) {
  13. this.prefix = prefix;
  14. }
  15. public String getSuffix () {
  16. return suffix;
  17. }
  18. public void setSuffix (String suffix) {
  19. this.suffix = suffix;
  20. }
  21. }

 创建业务代码,实现模拟的业务需求,它使用上面的配置类读取配置做业务处理(直接输出);


  
  1. package com.example.hello.service;
  2. import com.example.hello.bean.TestProperties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. public class HelloService {
  5. @Autowired
  6. TestProperties testProperties;
  7. public String sayHello () {
  8. return testProperties.getPrefix() + "<---- say hello --->" + testProperties.getSuffix();
  9. }
  10. }

再创建1个自动配置类TestAutoConfiguration.java,该类将被写入spring.factories中作为自动配置类在后面的测试springBoot工程启动中被扫描到IOC容器,该配置类将把上面的service服务实例化;


  
  1. package com.example.hello.config;
  2. import com.example.hello.service.HelloService;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. @ConditionalOnMissingBean(HelloService.class)
  8. public class TestAutoConfiguration {
  9. @Bean
  10. public HelloService makeHelloService (){
  11. return new HelloService();
  12. }
  13. }

spring.factories配置

在\resources\META-INF下创建spring.factories文本,指定当前测试stater包中的配置类:


  
  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. com.example.hello.config.TestAutoConfiguration

工程目录如下: 

 本地包上传

以上就完成业务逻辑,使用pom的clean+install命令把该starter工程上传本地,其他程序可以直接通过pom引入;

使用自定义starter依赖

再创建1个子模块test-my-project,作为真正的依赖上面的业务代码实体,它是个web应用:

 编辑其pom依赖如下,这里的重点是test01-spring-boot-starter的依赖工程,是我们上面本地上传的包;


  
  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.springframework.boot </groupId>
  5. <artifactId>spring-boot-dependencies </artifactId>
  6. <version>2.3.7.RELEASE </version>
  7. <type>pom </type>
  8. <scope>import </scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot </groupId>
  15. <artifactId>spring-boot-autoconfigure </artifactId>
  16. <scope>compile </scope>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot </groupId>
  20. <artifactId>spring-boot-starter </artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot </groupId>
  24. <artifactId>spring-boot-starter-web </artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.example </groupId>
  28. <artifactId>test01-spring-boot-starter </artifactId>
  29. <version>0.0.1-SNAPSHOT </version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot </groupId>
  33. <artifactId>spring-boot-starter-test </artifactId>
  34. <scope>test </scope>
  35. </dependency>
  36. </dependencies>

测试和配置

当前工程的application.properties写入以下配置:


  
  1. test.hello.prefix = prefix
  2. test.hello.suffix = suffix

直接使用注解使用starter包中的HelloService这个bean,它将在启动时候被IOC实例化;


  
  1. @RestController
  2. public class HelloController {
  3. @Autowired
  4. HelloService helloService;
  5. @GetMapping("/hello")
  6. public String sayHello (){
  7. return helloService.sayHello();
  8. }
  9. }

输出正常表明测试的starter包被成功引入,并且实现了业务代码的类被IOC实例化,在真实的工程中可以直接调用。


转载:https://blog.csdn.net/yezonggang/article/details/128646349
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场