前言
前面分析了SpringBoot的起步依赖与自动配置,本文将实现自定义起步依赖封装jedis类,去除不必要的配置简化开发。
前文参考:SpringBoot:起步依赖-自动配置_想吃肉蟹煲呀的博客-CSDN博客
实现
案例描述
第一个工程:自定义 spring-boot-starter-my-jedis项目,通过自动配置类创建Jedis对象,自动读取配置数据
第二个工程:引用上面的自定义启动器,修改一些默认配置,直接注入Jedis对象使用
第一个工程实现自动装配
创建第一个工程:spring-boot-starter-my-jedi
- 创建工程导入依赖
- 编写jedis 配置属性类 JedisProperties
- 编写自动配置类 JedisAutoConfiguration加载属性文件
-
- 创建连接池对象
- 注入配置数据对象JedisProperties
- 创建Jedis对象加入IOC容器
- 创建 META-INF/spring.factories 配置加载自动配置类 JedisAutoConfiguration
第一个工程实现
第一个工程结构:
- JedisAutoConfiguration:自动配置类:读取配置信息
- JedisProperties:属性配置
- spring.factories:指定加载哪个自动配置类
1、创建工程导入依赖
<?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> <groupId>org.example</groupId> <artifactId>spring-boot-starter-my-jedis</artifactId> <version>1.0-SNAPSHOT</version> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.5.0</version> </parent> <dependencies> <!-- jedis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
2、编写jedis 配置属性类 JedisProperties, 读取使用当前自动装配工程中的application.yml的配置数据
出现上面的提示不理会,如果有application.yml文件则读取,没有则使用默认值
package com.study.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 配置类:读取属性文件 */ @Data @ConfigurationProperties(prefix = "myjedis") public class JedisProperties { //配置默认参数,实际还会有另一个默认的元数据json文件 private String host = "127.0.0.1"; private Integer port = 6379; private Integer maxActive = 10; private Integer maxWait = 2000; }
3、编写自动配置类 JedisAutoConfiguration
package com.study.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; //标识当前类是配置类 @Configuration //当引用了Jedis.class类就运行当前自动配置类 @ConditionalOnClass(Jedis.class) //开启 JedisProperties.class读取yml文件 @EnableConfigurationProperties(JedisProperties.class) public class JedisAutoConfiguration { //定义连接池变量 private JedisPool jedisPool; public JedisAutoConfiguration(JedisProperties jedisProperties){ //创建配置对象 JedisPoolConfig config = new JedisPoolConfig(); //设置属性 config.setMaxTotal(jedisProperties.getMaxActive()); config.setMaxWaitMillis(jedisProperties.getMaxWait()); //创建连接池 jedisPool = new JedisPool(config, jedisProperties.getHost(), jedisProperties.getPort()); } @Bean public Jedis jedis(){ //从连接池中获取对象 return jedisPool.getResource(); } }
5、在当前项目reources目录下 创建 META-INF/spring.factories 配置加载自动配置类 MyJedisAutoConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.study.config.JedisAutoConfiguration
第二个工程:使用自动装配
- 创建工程pom.xml导入依赖,尤其导入上面启动器 spring-boot-starter-my-jedis 依赖
- 创建启动程序类 JedisDemoApplication
- application.yml配置jedis信息
- 创建测试类 JedisDemoTest 直接注入 Jedis 对象并使用,说明启动器自动配置创建Jedis对象是成功的。
第二个工程实现
工程结构
1、创建工程导入依赖,尤其导入上面启动器 spring-boot-starter-my-jedis 依赖,完成自动装配
<?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> <groupId>org.example</groupId> <artifactId>spring-auto-config-demo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.5.0</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 导入jedis的起步依赖完成自动配置 --> <dependency> <groupId>org.example</groupId> <artifactId>spring-boot-starter-my-jedis</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
2、创建启动程序类 JedisDemoApplication
package com.study; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JedisDemoApplication { public static void main(String[] args) { SpringApplication.run(JedisDemoApplication.class, args); } }
3、application.yml配置jedis信息
myjedis: # 没有写的则使用默认配置 max-active: 20 max-wait: 4000
4、创建测试类 JedisDemoTest 直接注入 Jedis 对象并使用,说明启动器自动配置创建Jedis对象是成功的。
package com.study.test; import com.study.config.JedisProperties; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import redis.clients.jedis.Jedis; @SpringBootTest public class JedisDemoTest { @Autowired private Jedis jedis; //注入redis,激活前一个工程的配置类 @Autowired private JedisProperties jedisProperties; //注入属性配置对象 @Test public void testJedis() { //使用Redis jedis.set("author", "想吃肉蟹煲呀"); String user = jedis.get("author"); //输出 System.out.println(user); //输出配置信息,可以删除application.yml文件中的属性,看属性是否有变化 System.out.println(jedisProperties); } }