文章目录

  • 自定义starter的作用
  • 一、自定义starter
  • 1、starter的命名
  • 2、创建maven项目 引入springboot的autoconfigure
  • 3、需要自动配置的业务Bean
  • 4、创建配置类,向容器类中注入第三步需要生成的Bean
  • 5、编写/META-INF/spring.factories供SpringBoot扫描
  • 二、测试自定义starter
  • 1、springboot项目中引入刚刚创建的starter
  • 2、springboot项目配置文件中,定义starter需要的配置文件
  • 3、直接尝试注入自动配置的Bean
  • 总结


自定义starter的作用

开发的时候,经常有一些独立于业务之外的配置模块。如果将这些可独立与业务的模块,配置成一个个的starter,复用的时候就更加方便了。只需要pom中引入,项目中就可以由springboot帮我们完成自动装配。关于自动装配,大家可以参考这篇博客 从注解入手扒一扒SpringBoot的自动配置原理


一、自定义starter

1、starter的命名

springboot官方提供的starter一般以spring-boot-starter-xxxx方式命名,官方建议自定义的starter使用xxxx-spring-boot-starter命名。用来区分第三方的starter和springboot官方的starter。

2、创建maven项目 引入springboot的autoconfigure

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.2.2.RELEASE</version>
  </dependency>
</dependencies>

3、需要自动配置的业务Bean

这里简单配置了一个javaBean,功能是读取配置文件中的属性,映射成这个Bean。但是仅仅这么配置,Spring扫描时,并不会注册这个Bean。因为Spring的启动过程中的自动配置,是查找classpath上所有jar包中的META-INF/spring.factories 和 .imports文件中的配置类,进行的自动装配。仅仅定义一个Bean是不会被扫描的。

@EnableConfigurationProperties(SimpleBean.class)
    @ConfigurationProperties(prefix = "simplebean")
    public class SimpleBean {
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "SimpleBean{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
        }
    }

4、创建配置类,向容器类中注入第三步需要生成的Bean

//@ConditionalOnClass:当类路径classpath下有指定的类的情况下进行自动配置,此处表示无限制,直接自动注册
@Configuration
@ConditionalOnClass
public class MyAutoConfiguration {
  static {
    System.out.println("MyAutoConfiguration init....");
 }
    
  @Bean
  public SimpleBean simpleBean(){
    return new SimpleBean();
 }
    
}

5、编写/META-INF/spring.factories供SpringBoot扫描



1.在resource下创建文件 /META-INF/spring.factories

spring boot 自定义流程 如何自定义springbootstarter_java


2.配置spring.factories中的EnableAutoConfiguration属性 。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lagou.config.MyAutoConfiguration


二、测试自定义starter



1、springboot项目中引入刚刚创建的starter

<dependency>
  <groupId>priv.lyp</groupId>
  <artifactId>zdy-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>



2、springboot项目配置文件中,定义starter需要的配置文件

simplebean.id=1
simplebean.name=自定义starter



3、直接尝试注入自动配置的Bean

//测试自定义starter
@Autowired
private SimpleBean simpleBean;
@Test
public void zdyStarterTest(){
	System.out.println(simpleBean);
}


总结

spring boot 自定义流程 如何自定义springbootstarter_spring_02


从mybatis提供的自定义的starter结构可以看出,starter就是一个普通的maven项目,引入了需要配置的模块后,帮用户写好接入Spring需要的配置操作,再进行一次封装。用户就可以省去配置的过程,做到引入就自动配置。这一点也符合Spring在官网上,对SpringBoot的介绍_ Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.