需求:
我们在一个项目中添加自定义起步依赖,无序额外配置即可使用jedis(开箱即用)
首先我们在mystarter自定义起步依赖model中加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.4</version>
</dependency>
<!--加入jedis坐标-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
然后新建一个类
package com.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
@Configuration
public class JedisAutoConfiguration {
@Bean
public Jedis jedis(){
return new Jedis("localhost",6379);
}
}
在resources里面添加这个文件META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.config.JedisAutoConfiguration
以上自定义起步依赖就好了
然后我们在另一个model中测试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--加入我们的自定义starter(起步依赖)-->
<dependency>
<groupId>org.example</groupId>
<artifactId>mystarter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
/*
* @SpringBootApplication
* 这个注解里面有@EnableAutoConfiguration又通过
* @Import(AutoConfigurationImportSelector.class)引入了AutoConfigurationImportSelector.class
*
* 所以会自动找META-INF/spring.factories
* */
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Demo1Application.class, args);
Jedis bean = applicationContext.getBean(Jedis.class);
System.out.println(bean);
}
}
--------------------------------------------------------
通过以上就完成了自定义起步依赖
优化一下配置
在自定义model中加入
package com.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.io.Serializable;
@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;
}
}
修改JedisAutoConfiguration类
package com.demo.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
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
/*
* 启用pojo,映射到yml文件中,并且交给spring容器进行管理
* */
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(Jedis.class)//当类路径下又Jedis.class这个整个注解配置才会生效
public class JedisAutoConfiguration {
@Bean
/*如果别人在自己代码中配置了就不要把这个bean加入容器*/
@ConditionalOnMissingBean(name = "jedis")
public Jedis jedis(RedisProperties redisProperties){
return new Jedis(redisProperties.getHost(), redisProperties.getPort());
}
}
----
在测试的model中application.yaml文件中配置
redis:
host: localhost
port: 6379
这样就不用把redis的ip和端口号写死在自定义起步依赖中了
|