1. 需求说明: 当加入redis客户端的坐标的时候,自动配置jedis的bean 加载到spring容器中; 2. 实现步骤: 1.创建工程 it-redis-springboot-starter 用作起步依赖 2.添加依赖 1. pom文件 <?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>com.it</groupId> <artifactId>it-redis-springboot-starter</artifactId> <version>1.0-SNAPSHOT</version> <description>自定义起步依赖,给别人用,需要在这个项目中进行自动配置</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> <!--springboot的starter--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--redis的依赖jedis--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency> </dependencies> </project> 3.创建自动配置类和POJO 1. 创建pojo,并在配置类中使用注解@EnableConfigurationProperties(value = RedisProperties.class)建立映射: package com.it.redis.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import java.io.Serializable; /** * ToDo * * @author Lyle * @date 2020/4/2 */ @ConfigurationProperties(prefix = "redis") public class RedisProperties implements Serializable { private String host = "localhost";//给与默认值 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; } } 2. 创建配置类: package com.it.redis.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.Jedis; /** * ToDo * * @author Lyle * @date 2020/4/2 */ @Configuration//表是这是一个配置类 @EnableConfigurationProperties(value = RedisProperties.class) public class RedisAutoConfiguration { @Autowired private RedisProperties redisProperties; //注册类,交给spring容器 @Bean public Jedis jedis(){ System.out.println("该方法已经执行。。。。"); System.out.println(redisProperties.getHost()+"<=======>"+redisProperties.getPort()); return new Jedis(redisProperties.getHost(),redisProperties.getPort()); } } 3. 在resources下创建文件夹:META-INF,并在META-INF下创建文件spring.factories,文件中设置如下信息: 第一行信息为EnableAutoConfiguration的全限定路径名; 第二行为RedisAutoConfiguration的全限定路径; \:表示换行; org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.it.redis.config.RedisAutoConfiguration 4.创建工程 it-test-starter 用于测试使用: 4.1 测试类的pom文件: <?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>com.it</groupId> <artifactId>it-test-starter</artifactId> <version>1.0-SNAPSHOT</version> <description>依赖自定义的starter,可以直接使用jedis</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>com.it</groupId> <artifactId>it-redis-springboot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project> 4.2 创建启动类; package com.it; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import redis.clients.jedis.Jedis; /** * ToDo * * @author Lyle * @date 2020/4/2 */ @SpringBootApplication public class JedisStarterApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(JedisStarterApplication.class, args); Jedis jedis = context.getBean(Jedis.class); System.out.println(jedis); } } 4.3 配置文件application.properties(可以不配置,使用pojo中的默认配置) redis.host=localhost redis.port=666
原文:https://www.cnblogs.com/lyle-liu/p/12622271.html