美文网首页
spring Boot自定义start

spring Boot自定义start

作者: 全满 | 来源:发表于2021-01-25 16:03 被阅读0次
  • start(场景启动器)
  1. 这个场景需要使用到的依赖是什么?

也就是我们需要导入哪些jar包

  1. 如何编写自动配置
    WebMvcAutoConfiguration为例
@Configuration // 指定这个类是个配置类
@ConditionalOnXXX // 在指定条件成立的情况下自动配置类生效
@AutoConfigureOrder //配置类顺序
@AutoConfigureAfter // 在哪个配置类之后
@Bean //给容器中添加组件

@ConfigurationProperties //结合相关的XXXProperties类 来绑定相关的配置
@EnableConfigurationProperties // 让XXXProperties加入到容器中,别人就可以自动装配

// 自动配置类要能加载,有一个要求,源码分析结果是,需要在\META-INF\spring.factories中做如下配置
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  1. 模式(研究springBoot是怎么编写的,我们仿照着写)


    image.png

启动器只用来做依赖导入
专门写一个自动配置模块
启动器依赖自动配置模块,别人只需要引入启动器即可(starter)

  • 示例


    image.png
image.png image.png image.png image.png
  • starter项目中只需要引入start-autoconfiguration的包即可
    <dependencies>
        <dependency>
            <groupId>com.saikie.starter</groupId>
            <artifactId>example-spring-boot-starter-autoconfigurator</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
  • start-autoconfiguration项目中需要引入spring-boot基本start包
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
  • 删除start-autoconfiguration启动类及配置文件
  • 模拟一个sayHello的场景
  1. 首先创建一个service
public class HelloService {

    /**
     * 生产HelloProperties的getSet方法,使得配置类可以得到它
     */
    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();
    }
}
  1. 创建属性文件类HelloProperties(即实体,只是做了配置文件的绑定(使用@ConfigurationProperties注从Properties/yml文件中获取对应属性的值))
@ConfigurationProperties(prefix = "example.hello") //  绑定配置文件中所有以example.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;
    }
}
  1. 创建配置文件类
@Configuration // 标志为注解类
@ConditionalOnWebApplication //是web类的时候加载
@EnableConfigurationProperties(HelloProperties.class) // 使得HelloProperties属性文件生效
public class HelloServiceAutoConfiguration {

    // 导入属性文件
    @Resource
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService() {
        HelloService service = new HelloService();
        service.setHelloProperties(helloProperties);
        return service;
    }
}
  1. 将上述两个项目进行本地maven安装


  2. 测试start是否可用(创建一个springboot项目,此处省略)
    1). 首先引入start包
        <dependency>
            <groupId>com.saikie</groupId>
            <artifactId>example-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
image.png

2). 创建cotroller访问start的sayhello方法

@RestController
public class HelloController {

    @Resource
    HelloService helloService;

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

3). 编写配置文件中的前缀后缀

example.hello.prefix = "EXAMPLE"
example.hello.suffix = "HELLO"

4). 启动及结果

image.png
源码

相关文章

网友评论

      本文标题:spring Boot自定义start

      本文链接:https://www.haomeiwen.com/subject/iborzktx.html