天天看点

springboot2.x自定义starter(启动组件,配置参数,自动配置类)

springboot的核心就是为我们提供了许多的自动配置类

我们要自己定义我们的starter:

先确定~

1、这个场景需要使用到的依赖是什么?

2、如何编写自动配置

模式:

启动器只用来做依赖导入;

专门来写一个自动配置模块;

启动器依赖自动配置;别人只需要引入启动器(starter)

模块命名:

1:启动模块 xxx-spring-boot-starter

2:自动配置模块:xxx-spring-boot-starter-autoconfigrer

场景:我需要一个sayHello的service,里面有个sayHello’方法,方法参数就是String name;给谁说,然后可以通过配置前后坠输出我们想要的招呼信息:

创建一个新的项目:new project -> Empty Project //创建一个空的项目 需要添加模块

springboot2.x自定义starter(启动组件,配置参数,自动配置类)

第一个模块是作为starter模块 创建一个普通maven模块

springboot2.x自定义starter(启动组件,配置参数,自动配置类)

第二步创建一个springboot模块 不导入任何依赖starter 只是用默认的starter

springboot2.x自定义starter(启动组件,配置参数,自动配置类)

第三步 把autoconfigrer的模块依赖到starter模块 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yajunsoso</groupId>
    <artifactId>yajun-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    引入自动配置文件 我们自己的atuo-->

    <dependencies>
        <dependency>
            <groupId>com.yajunsoso</groupId>
            <artifactId>yajun-spring-boot-starter-autoconfigrer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
           

autoconfigrer模块的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yajunsoso</groupId>
    <artifactId>yajun-spring-boot-starter-autoconfigrer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>yajun-spring-boot-starter-autoconfigrer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
    <!-- 这里是普通的maven plugin 不然会报错,因为没有启动类,-->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

           

代码编写://看目录结构 这里没有的都删掉 包括启动类和配置文件、测试模块。

springboot2.x自定义starter(启动组件,配置参数,自动配置类)

注解解释:

springboot2.x自定义starter(启动组件,配置参数,自动配置类)
public class HelloService {
    HelloPropertice helloPropertice;

    public HelloPropertice getHelloPropertice() {
        return helloPropertice;
    }

    public void setHelloPropertice(HelloPropertice helloPropertice) {
        this.helloPropertice = helloPropertice;
    }

    public String sayHello(String name) {
        return helloPropertice.getPrefix() + "-" + name + "-" + helloPropertice.getSubfix();
    }
}

           
**
 * helloService的配置类
 */
@ConfigurationProperties(value = "yajun.hello")
public class HelloPropertice {
    private String prefix;

    private String subfix;

    public String getPrefix() {
        return prefix;
    }

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

    public String getSubfix() {
        return subfix;
    }

    public void setSubfix(String subfix) {
        this.subfix = subfix;
    }
}

           
@Configuration
@ConditionalOnWebApplication//web程序才成效
@EnableConfigurationProperties(HelloPropertice.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloPropertice helloPropertice;
   @Bean
    public HelloService helloService() {
       HelloService helloService = new HelloService();
       helloService.setHelloPropertice(helloPropertice);
       return helloService;
   }
}

           

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yajunsoso.service.HelloServiceAutoConfiguration
           
springboot2.x自定义starter(启动组件,配置参数,自动配置类)

最后一定要把这两个模块先后顺序的install的本地仓库,

然后就可以在其他项目maven中引入坐标 使用了

springboot2.x自定义starter(启动组件,配置参数,自动配置类)

这里就出现了我们自定以的starter和autoconfigrer了

springboot2.x自定义starter(启动组件,配置参数,自动配置类)
@Controller
public class HelloServiceController {
    //导入自定义的starter
    @Autowired
    HelloService helloService;

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