Starter 组件是 Spring boot 的一个核心特性,Starter组件的出现极大的简化了项目开发,例如在项目中使用的pom.xml文件中添加以下配置:
org.springframework.boot spring-boot-starter-web 2.5.3
Spring boot 就会自动关联web 开发相关的依赖,如 tomcat 及 spring-mvc等,进而能支持web开发,同时相关技术也将实现自动配置,避免了繁琐的配置文件。
Starter 组件 使开发者不需要关注各种依赖库的处理、不需要具体的配置信息,由Spring boot 自动完成class的发现并加载。
利用Starter实现自动化配置需要两个条件:Maven依赖以及配置文件。
Maven依赖实际上就是导入一个JAR,然后Springboot启动时候就会找到这个jar包中 resource/META-INF/spring.factories文件,根据文件的配置找到相关的实现类。以上就是spring boot 的自动装配机制,如果理解该机制的话,很容易就能手写一个 starter 组件。
对starter组件我们可以简单理解为:
要实现一个自己的 starter 组件其实也很简单,首先我们需要明确我们需要做那些事:
接下来我们就手写一个 starter 组件,模仿 RedisTemplate
,流程如下:
创建一个spring boot 项目 redis-starter-demo
创建MyStarterTemplate
简单模拟 redis 的 get
,set
操作:
public class MyStarterTemplate {Map map = new HashMap<>();public String put(String key, String value) {map.put(key, value);return "保存成功";}public String get(String key) {return map.get(key);}}
创建配置类
@Configuration
public class MyAutoConfiguration {@Beanpublic MyStarterTemplate myStarterTemplate(){return new MyStarterTemplate();}}
实现自动装配流程
在 classpath 下 META-INF目录下创建 spring.factories 文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.example.redisstarterdemo.MyAutoConfiguration
打包发布 maven install
创建一个新项目用来引入 starter test-demo
pom 文件引入依赖
com.example redis-starter-demo 0.0.1-SNAPSHOT
编写接口 ,直接 Autowired
注入MyStarterTemplate
@RestController
@RequestMapping(value = "/myRedis")
public class RedisStarterController {@Autowiredprivate MyStarterTemplate myStarterTemplate;@RequestMapping("/save")public String save(@RequestParam String key, @RequestParam String value) {myStarterTemplate.put(key,value);return "ok";}@RequestMapping("/get")public String get(@RequestParam String key) {return myStarterTemplate.get(key);}}
以上便是一个完成的自定义 Starter,如果搞懂SPI机制的话,现在应该觉得很简单单了吧?
以上是一个最简单的 starter 组件,但是有这么一种情况,我们还以 redis 为例,在 redis 中我们是可以通过在配置文件中来设置地址、账号密码等信息的,那这种情况我们应该如何操作呢?
我们还是在上面的项目中继续添加代码
创建需要注入的 bean 类
这里我们通过对外暴露一个创建方法用来给对象传递地址和端口号;
public class MyRedis {public String say() {return "my host:" + host + ",port:" + port;}public static MyRedis create(String host, Integer port) {return new MyRedis(host, port);}public MyRedis(String host, Integer port) {this.host = host;this.port = port;}private String host;private Integer port;public String getHost() {return host;}public void setHost(String host) {this.host = host;}public Integer getPort() {return port;}public void setPort(Integer port) {this.port = port;}
}
创建属性类
@ConfigurationProperties(prefix = "redis")
的作用是读取 application
中以 redis 开头的数据,还有其他几种读取配置文件的注解可以自行研究。
由于我们这里设置了默认值,所以如果不配置的话也不影响,会使用默认值传递给 bean 。
@ConfigurationProperties(prefix = "redis")
public class MyProperties {private String host = "127.0.0.1";private Integer port = 6379;public String getHost() {return host;}public void setHost(String host) {this.host = host;}public Integer getPort() {return port;}public void setPort(Integer port) {this.port = port;}}
创建配置类
使用配置文件,将相关信息传递给 bean 类。
@Configuration
@EnableConfigurationProperties(MyProperties.class)
public class MyRedisConfiguration {@Beanpublic MyRedis myRedis(MyProperties properties) {return MyRedis.create(properties.getHost(), properties.getPort());}}
实现自动装配流程
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.example.redisstarterdemo.demo1.MyAutoConfiguration,\com.example.redisstarterdemo.demo2.MyRedisConfiguration
在项目中引用还是同样的操作,如果想要设置属性的话,只需要在配置文件中编写相关信息即可:
redis.host=196.128.255.255
redis.port=9999
针对 spring boot 的starter 组件分为两类:
@ConditionalOnClass(RedisOperations.class)
在classpath下找不到RedisOperations
类,就不会加载该配置类。