赞
踩
01: SpringBoot 高级 原理分析 – SpringBoot 自动配置:Condition
02: SpringBoot 高级 原理分析 – 切换内置web服务器
03: SpringBoot 高级 原理分析 – @Enable*注解、@Import注解
04: SpringBoot 高级 原理分析 – @Import注解
05: SpringBoot 高级 原理分析 – @EnableAutoConfiguration 注解
06: SpringBoot 高级 原理分析 – 自定义redis-starter
需求:
自定义redis-starter。要求当导入redis坐标时,SpringBoot自动创建Jedis的Bean。
引入
mybatis-spring-boot-starter
pom.xml
<!--引入mybatis-stater-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
参照上面的mybatis-spring-boot-starter起步依赖,我们分析出了自定义redis-starter的思路
依赖 redis-springboot-autoconfigure的模块:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tian</groupId> <artifactId>redis-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-spring-boot-starter</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 引入自定义的redis-spring-boot-autoconfigure--> <dependency> <groupId>com.tian</groupId> <artifactId>redis-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tian</groupId> <artifactId>redis-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> <name>redis-spring-boot-autoconfigure</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 引入jedis坐标--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies> </project>
RedisAutoConfiguration.java
package com.tian.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.Jedis; @Configuration // 声明该类为配置类 @EnableConfigurationProperties(RedisProperties.class)// 让Spring识别RedisProperties里面定义的配置信息 @ConditionalOnClass(Jedis.class)// 只有依赖库中有Jedis.class才会 往IOC中注入redis的bean public class RedisAutoConfiguration { /** * 提供Jedis的bean */ @Bean // 注入bean @ConditionalOnMissingBean(name = "jedis") // 只有在IOC容量里面没有名为jedis的bean时 才注入名为jedis的bean public Jedis jedis(RedisProperties redisProperties) { return new Jedis(redisProperties.getHost(), redisProperties.getPort()); } }
RedisProperties.java
package com.tian.config; import org.springframework.boot.context.properties.ConfigurationProperties; // 配置文件中以redis开头的属性都会和本地的redis对应起来 @ConfigurationProperties(prefix = "redis") public class RedisProperties { // 默认为本机 private String host = "localhost"; // redis默认端口为6379 private int port = 6379; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
定义META-INF/spring.factories文件:
说明,这里\
表示换行。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tian.config.RedisAutoConfiguration
创建一个测试SringBoot测试模块:
编写测试程序:
package com.tian; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import redis.clients.jedis.Jedis; @SpringBootApplication public class SpringbootEnableApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args); Jedis jedis = (Jedis) context.getBean("jedis"); System.out.println("jedis: "+jedis); } }
运行结果:
进行存储数据测试:
测试配置是否生效:
我们改了参数之后redis就运行不起来了,说明我们的配置生效了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。