SpringBoot的自定义starter

自定义starter

starter:
1、这个场景需要的依赖是什么?
2、如何编写自动配置

启动流程

1、创建SpringApplication对象
2、如何编写自动配置

@Configuration//指定这是一个配置类
@ConditionalOnXXX//指定条件成了的情况下自动配置类生效
@AutoConfigureOrder//指定自动配置类的顺序
@Bean//在容器中添加组件

@ConfigurationProperties//结合相关的XXXProperties类来绑定相关的配置
@EnableConfigurationProperties//让XXXproperties类生效,并且加入到容器中

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\

3、模式
启动器(starter)只用来做以来导入:专门用来写一个自动配置模块,启动器依赖自动配置,其他人只需要引入启动器就ok了。
自定义命名:

  • -后缀:“*-spring-boot-starter”
  • -模式:“*-spring-boot-starter”

实际操作

1、首先我们新建一个空项目
在这里插入图片描述
2、在这个空项目里添加modules【模块】
在这里插入图片描述
3、先添加一个maven模块
在这里插入图片描述
4、再添加一个springboot模块
在这里插入图片描述
5、完成两个模块的添加
在这里插入图片描述
6、开始编码
最基础的东西我们需要新增以下几个文件
在这里插入图片描述
HelloProperties
定义了Properties的配置项的内容,前缀需要用atlyb.hello来启用

@ConfigurationProperties(prefix = "atlyb.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;
    }
}

HelloService

public class HelloService {

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

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

HelloServiceAutoConfiguration
@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效
也可以在@ConfigurationProperties 使用 @Component 注册,不过相对麻烦。
如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。

@Configuration
@ConditionalOnWebApplication//如果是web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {

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

编写autoconfigure的spring.properties工厂
注意此处必须是META-INF下的spring.properties文件夹,在一开始的时候说明过,自动注入的原理就是在寻找这个路径下的配置注入信息。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.atlyb.starter.HelloServiceAutoConfiguration

starter的pom中引入autoconfigure的包


    <!--启动器-->
    <dependencies>

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

=============================================================================================
7、以上几步就完成了一个简单的自定义starter接下来我们通过maven的lifecircle中的install来安装,要按照依赖顺序安装,首先是autoconfigure然后是starter
在这里插入图片描述
可以看到就装到了我们的本地库
在这里插入图片描述
8、编写测试
我们新建一个springboot工程。把刚才的生成的maven包引入。
在这里插入图片描述
写一个controller方法

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHello("habi");
    }

}

之前我们定义的前后的方法都是通过配置加入的。
在这里插入图片描述
9、在properties方法中写入我们的配置

atlyb.hello.prefix= goodpre
atlyb.hello.suffix= goodsuf

10、运行起来
在这里插入图片描述

自定义starter完成

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值