SpringBoot源码学习之路(十七、自定义 starter(启动器))

自定义starter(启动器)

starter:

一、SringBoot中的Starter分析:

1、一般开发中经常还会遇见许多场景是我们经常会使用到的,那我们可以将其作为自定义的Starter。实现这些场景自动配置。其他应用使用时只需要导入依赖即可。

2、如何实现场景自动配置呢?我们参考下SpringBoot中的自动配置实现流程:

①、编写一个自动配置类

@Configuration     //指定这个类是一个配置类
@ConditionalOnXXX  //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter//指定自动配置类的顺序
@Bean              //给容器中添加组件
@ConfigurationPropertie       //结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties//让xxxProperties生效加入到容器中

②、自动配置要生效需要配置在META-INF/spring.factories

#类型指定为自动配置接口
#第二行配置具体实现类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\

3、Starter启动器的实现模式:
①、启动器只用来做依赖导入,即整个模块只需要导入其依赖的jar;
②、编写一个自动配置模块,以jar方式打包;
③、启动器依赖自动配置模块,用户只需要引入启动器(starter)就可以实现自动配置;
④、启动器模块命名规则:XXX(自定义启动器名)-spring-boot-starter(如:mybatis-spring-boot-starter;)
⑤、自动配置模块命名规则:XXX(自定义启动器名)-spring-boot-starter-autoconfigurer


二、实现自定义starter


   场景:目前某个SpringBoot应用,有一个对外的请求处理,即当用户访问 " /hello?name = xxx  "时,其需要返回字符串,
   " AAA xxx BBB ",即在参数name前加上 `prefix`:AAA 和 后面加上 `suffix` : BBB。
   并且AAA、BBB的值可配置在application.yml配置文件中。

即需要有个启动器可以将我们需要实现我们要的业务场景,并把业务类自动配置进容器中,方便其他应用使用从容器中获取使用。


1、自动配置模块实现:

(1)、创建自动配置模块,并引入spring-boot-starter(所有starter的基本配置)
<?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.wanngcw.starter</groupId>
   <artifactId>wanngcw-spring-boot-starter-autoconfigurer</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>wanngcw-spring-boot-starter-autoconfigurer</name>
   <description>Wanngcw Autoconfigurer Module for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.10.RELEASE</version>
      <relativePath/>
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <!--引入spring-boot-starter;所有starter的基本配置-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>
   </dependencies>

</project>
(2)、指定对应的XxxProperties类,用于定义可配置的值。
import org.springframework.boot.context.properties.ConfigurationProperties;

//需要加入该注解指定配置前缀
@ConfigurationProperties(prefix = "wangcw.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;
    }
}
(3)、定义业务类,用于获取用户配置信息,并且实现我们的业务场景。
public class HelloService {

    private HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

    /* 实现我们的业务场景 */
    public String appendString(String name){
        return helloProperties.getPrefix()+":" + name + ":" + helloProperties.getSuffix();
    }
}
(4)、定义自动配置类,并且需要将其我们的业务类实例化置于容器中。
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 HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}
(5)、要让自动配置类能生效,需要在’resources\META-INF\spring.factories’文件配置出来。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wangcw.starter.HelloServiceAutoConfiguration
(6)、执行mvn install安装到仓库中。

2、Starter启动器模块实现:

(1)、创建启动器配置模块,并引入wanngcw-spring-boot-starter-autoconfigurer自动配置的依赖。

<?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.wangcw.starter</groupId>
    <artifactId>wangcw-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--启动器-->
    <dependencies>
        <!--引入自动配置模块-->
        <dependency>
           <groupId>com.wanngcw.starter</groupId>
           <artifactId>wanngcw-spring-boot-starter-autoconfigurer</artifactId>
           <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>
(2)、执行mvn install安装到仓库中。到此,其他用户就可以通过引入该Starter依赖,

然后从容器中获取HelloService组件实现该业务。


3、测试自定义的starter

(1)、新建SpringBoot应用,并导入web依赖和我们自定义的依赖wangcw-spring-boot-starter
<?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.wangcw.springbootdemo</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springbootdemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 引入web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入自定义的starter-->
        <dependency>
            <groupId>com.wangcw.starter</groupId>
            <artifactId>wangcw-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
(2)、在配置为文件中配置相关信息
wangcw:
  hello:
    prefix: Welcome
    suffix: ----HelloWorld!
(3)、编写Controller
@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello(String name){
        return helloService.appendString(name);
    }
}
(4)、启动并访问测试:

这里写图片描述

更多SpringBoot整合示例

SpringBoot官方提供了许多整合其他框架的源码,都在其GitHub中:
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

猜你喜欢

转载自blog.csdn.net/qq_33404395/article/details/81208374
今日推荐