首页 > IT职业 > IT图书 > 自定义Spring Boot Starter
在使用Spring Boot开发的时候,我们会用到各种各样的Spring-boot-Starter,不过那些Starter都是常用的,在不同的公司,因不同的业务场景,内部的库也不同,有时需要做一个自己的starter,方便后面加快开发速度。
做一下演示,内部的代码不方便公开。
比如RedisProperties
我们新建TestProperties,这里我省略了getter和setter,自己写的时候加上
@Data
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private String name;
private String desc;
private Nest nest;
public static class Nest {
private String nestName;
}
} 参考Redis的
我们自己的
@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestAutoConfigurer {
private TestProperties testProperties;
public TestAutoConfigurer(TestProperties testProperties) {
this.testProperties = testProperties;
}
...
}
建立好上面两个类之后,根据业务建立,以及配置的属性就可以建立Bean了。一般会用到@conditonalOnMissingBean(xxBean.class)
test:
name: aihe
nest:
nestName: nestAihe

可以看到已经注入进来了。
假如我们的自动配置类,不在当前包下怎么办呢?
在resource目录下新建这两个文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
me.aihe.ShiroCasAutoConfigurer
这个文件主要分为两部分,一个groups一个properties,其余的因为是json格式,比较容易理解,自己写就好了

主要介绍了下Spring Boot starter的简单制作,感兴趣的可以自己试试。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2041/viewspace-2806938/ ,如需转载,请注明出处,否则将追究法律责任。