本文以jedis作为演示
命名规范:
spring官方starter通常命名为spring-boot-starter-{name}, 如:spring-boot-starter-web
spring官方建议非官方starter命名应遵循{name}-spring-boot-starter的格式, 如:mybatis-spring-boot-starter
- 创建maven jar工程,引入依赖
<?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>
<artifactId>jedis-spring-boot-starter</artifactId>
<groupId>demo.springboot</groupId>
<version>1.0-SNAPSHOT</version>
<name>jedis-spring-boot-starter</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 自定义starter的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<!-- jedis的依赖 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
- 创建一个获取配置文件属性的类
//获取配置文件中的属性
@ConfigurationProperties(prefix = "jedis")
public class JedisProperties {
private int database = 0;
private String host = "localhost";
private String password = "";
private int port = 6379;
public int getDatabase() {
return database;
}
public void setDatabase(int database) {
this.database = database;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
- 创建自动装配bean的配置类
@Configuration
//当类路径下有Jedis依赖时进行配置
@ConditionalOnClass(Jedis.class)
//引入JedisProperties对象
@EnableConfigurationProperties(JedisProperties.class)
public class JedisAutoConfiguration {
@Bean
//当spring容器中没有该Bean时注册
@ConditionalOnMissingBean(Jedis.class)
public Jedis jedis(JedisProperties jedisProperties){
//设置地址端口
Jedis jedis = new Jedis(jedisProperties.getHost(), jedisProperties.getPort());
//验证密码
jedis.auth(jedisProperties.getPassword());
//设置索引库
jedis.select(jedisProperties.getDatabase());
jedis.connect();
return jedis;
}
}
更多特定注解可从官网或者搜索引擎中搜索
-
创建spring.factories
没有此文件配置,自动配置类不会生效。将创建的配置类路径填写到里面
此文件需要自己手动创建,位置:src/main/resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=demo.springboot.JedisAutoConfiguration
打包 mvn install
-
创建一个springboot项目测试
- application.yml
#不设置配置,默认使用JedisProperties对象的默认值
jedis:
host: localhost
port: 6379
database: 0
password: root
- 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTests {
@Autowired
private Jedis jedis;
@Test
public void testJedisStarter(){
jedis.set("test", "hello spring boot starter");
}
}
- 运行完成,查看redis中是否添加了一条数据
localhost:0>get test
"hello spring boot starter"
查到的数据就是测试中添加的数据
作者公众号