SpringBoot——自定义Starter

1.背景知识

1.1启动器与自动配置类

  • 启动器只用来做依赖导入
  • 专门写一个自动配置类
  • 启动器依赖自动配置类,项目引入相应的starter就会引入启动器的所有的传递依赖

SpringBoot——自定义Starter_第1张图片

1.2 启动器

启动器是一个空Jar文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库

命名规约

  • 官方命名

             spring-boot-starter-模块名(eg:spring-boot-starter-web,spring-boot-statrer-jdbc)

  • 自定义命名

模块名-spring-boot-starter(eg:mybatis-spring-boot-starter)

1.3 如何编写自动配置

@Cinfiguration //指定这个类是一个自动配置类
@ConditionnalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigurAfer //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationProperties//结合xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
public class XxxAutoConfiguration(){

自动配置要能加载,需要将启动就加载的自动类配置在META-INF/spring.factories中。

例如:

org.springframework.context.ApplicationContextInitializer=\
  com.example.bootjpa.listener.HelloApplicationContextInitializer
org.springframework.boot.SpringApplicationRunListener=\
  com.example.bootjpa.listener.HelloSpringApplicationRunListener

2.案例

1.创建一个空工程

2.创建一个自动配置模块,和创建其他的普通的SpringBoot项目一样,不需要引入其他starter

3.删除多余的文件和依赖



    4.0.0
    com.test.starter
    spring-boot-starter-autoconfigure
    0.0.1-SNAPSHOT
    spring-boot-starter-autoconfigure
    Demo project for Spring Boot

    
        1.8
        UTF-8
        UTF-8
        2.3.0.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter
        


    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    




4.创建配置类和自动配置类

xxxProperties:

@Component
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

xxxService:

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix()+"-"+ name+helloProperties.getSuffix();
    }
}

xxxAutoConfiguration:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication //web应用才生效
@EnableConfigurationProperties(HelloProperties.class) //让配置类生效(注入到容器中)
public class HelloAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
        HelloService helloService=new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.test.starter.HelloAutoConfiguration

6.安装到本地仓库:Lifecycle——》install

SpringBoot——自定义Starter_第2张图片

7.创建starter,选择Maven工程即可,只是用于管理依赖,添加对AutoConfiguration模块的依赖



    4.0.0

    com.test.starter
    springboot-starter
    1.0-SNAPSHOT

    
    
        
        
            com.test.starter
            spring-boot-starter-autoconfigure
            0.0.1-SNAPSHOT
        

    

8.安装到本地仓库:Lifecycle——》install

9.创建SpringBoot项目进行测试,选择添加Web场景



    4.0.0
    com.test
    spring-boot-starter-test
    0.0.1-SNAPSHOT
    spring-boot-starter-test
    Demo project for Spring Boot

    
        1.8
        UTF-8
        UTF-8
        2.3.0.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.test.starter
            springboot-starter
            1.0-SNAPSHOT
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


10.创建controller


@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
       return helloService.sayHello("hello");
    }
}

11.在application.properties配置文件中配置

hello.prefix=Hello
hello.suffix=World

12.启动项目访问:http://localhost:8080/hello

你可能感兴趣的:(SpringBoot)