ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HM-SpringBoot2.6.2【SpringBoot自动配置-自定义starter案例】

2021-08-02 15:05:13  阅读:160  来源: 互联网

标签:SpringBoot2.6 return SpringBoot 自定义 springframework class org import public



 1 实现

 1 package com.haifei.redis.config;
 2 
 3 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import redis.clients.jedis.Jedis;
 7 
 8 @Configuration
 9 @EnableConfigurationProperties(RedisProperties.class)
10 public class RedisAutoConfiguration {
11 
12     /**
13      * 提供jedis的bean
14      */
15     @Bean
16     public Jedis jedis(RedisProperties redisProperties){
17 //        return new Jedis();
18 //        return new Jedis("localhost", 6379);
19         return new Jedis(redisProperties.getHost(), redisProperties.getPort()); //动态指定参数
20     }
21 
22 }
 1 package com.haifei.redis.config;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 5 
 6 /*
 7 @ConfigurationProperties 在IDEA中出现红色波浪线问题解决
 8 https://blog.csdn.net/qq_19309473/article/details/106859603
 9 第一步:在pom.xml中增加以下依赖spring-boot-configuration-processor
10 第二步:在@ConfigurationProperties的上方增加一个@EnableConfigurationProperties({当前类.class})
11  */
12 //@EnableConfigurationProperties({RedisProperties.class})
13 //后记:将这个注解加在RedisAutoConfiguration配置类上也行,且不加spring-boot-configuration-processor依赖也行
14 
15 @ConfigurationProperties(prefix = "redis") //与配置文件绑定。配置文件中以redis开头的配置都会与当前实体类中的属性相对应
16 public class RedisProperties {
17 
18     private String host = "localhost"; //默认主机为本机
19     private int port = 6379; //默认端口为6379
20 
21     @Override
22     public String toString() {
23         return "RedisProperties{" +
24                 "host='" + host + '\'' +
25                 ", port=" + port +
26                 '}';
27     }
28 
29     public String getHost() {
30         return host;
31     }
32 
33     public void setHost(String host) {
34         this.host = host;
35     }
36 
37     public int getPort() {
38         return port;
39     }
40 
41     public void setPort(int port) {
42         this.port = port;
43     }
44 }

 

 

 

 

1 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2   com.haifei.redis.config.RedisAutoConfiguration

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 1 package com.haifei.springboot9enable;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 import redis.clients.jedis.Jedis;
 7 
 8 @SpringBootApplication
 9 public class Springboot9EnableApplication3 {
10 
11     public static void main(String[] args) {
12         ConfigurableApplicationContext context = SpringApplication.run(Springboot9EnableApplication3.class, args);
13         Jedis jedis = context.getBean(Jedis.class);
14         System.out.println(jedis); //BinaryJedis{Connection{DefaultJedisSocketFactory{localhost:6379}}}
15     }
16 
17 }

 

 


 2 优化

(1)

 

 

 

 

 1 package com.haifei.springboot9enable;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 import redis.clients.jedis.Jedis;
 7 
 8 @SpringBootApplication
 9 public class Springboot9EnableApplication3 {
10 
11     public static void main(String[] args) {
12         ConfigurableApplicationContext context = SpringApplication.run(Springboot9EnableApplication3.class, args);
13         Jedis jedis = context.getBean(Jedis.class);
14         System.out.println(jedis); //BinaryJedis{Connection{DefaultJedisSocketFactory{localhost:6379}}}
15 
16         jedis.set("name", "zhangsan");
17         String name = jedis.get("name");
18         System.out.println(name); //zhangsan
19     }
20 
21 }

 

 

 

 

 

(2) 

验证配置生效

 

 

 

 

(3)

 1 package com.haifei.redis.config;
 2 
 3 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 4 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 5 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.Configuration;
 8 import redis.clients.jedis.Jedis;
 9 
10 @ConditionalOnClass(Jedis.class) //加条件:保证Jedis存在的时候才可以加在下面配置的这个jedis的bean
11 
12 @Configuration
13 @EnableConfigurationProperties(RedisProperties.class)
14 public class RedisAutoConfiguration {
15 
16     /**
17      * 提供jedis的bean
18      */
19     @Bean
20     @ConditionalOnMissingBean(name = "jedis")
21     //加条件:若用户自己也定义了一个叫jedis的bean,则就用用户自己定义的,若没有一个叫jedis的bean时,我们才向用户提供该bean
22     public Jedis jedis(RedisProperties redisProperties){
23 //        return new Jedis();
24 //        return new Jedis("localhost", 6379);
25 
26         System.out.println("RedisAutoConfiguration....");
27         return new Jedis(redisProperties.getHost(), redisProperties.getPort()); //动态指定参数
28     }
29 
30 }

 

 

 1 package com.haifei.springboot9enable;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 import redis.clients.jedis.Jedis;
 7 
 8 @SpringBootApplication
 9 public class Springboot9EnableApplication3 {
10 
11     public static void main(String[] args) {
12         ConfigurableApplicationContext context = SpringApplication.run(Springboot9EnableApplication3.class, args);
13         Jedis jedis = context.getBean(Jedis.class);
14         System.out.println(jedis); //BinaryJedis{Connection{DefaultJedisSocketFactory{localhost:6379}}}
15 
16         jedis.set("name", "zhangsan");
17         String name = jedis.get("name");
18         System.out.println(name); //zhangsan
19     }
20 
21 }

 

 

 

 

 1 package com.haifei.springboot9enable;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 import org.springframework.context.annotation.Bean;
 7 import redis.clients.jedis.Jedis;
 8 
 9 @SpringBootApplication
10 public class Springboot9EnableApplication3 {
11 
12     public static void main(String[] args) {
13         ConfigurableApplicationContext context = SpringApplication.run(Springboot9EnableApplication3.class, args);
14         Jedis jedis = context.getBean(Jedis.class);
15         System.out.println(jedis); //BinaryJedis{Connection{DefaultJedisSocketFactory{localhost:6379}}}
16 
17         jedis.set("name", "zhangsan");
18         String name = jedis.get("name");
19         System.out.println(name); //zhangsan
20     }
21 
22     @Bean
23     public Jedis jedis(){
24         System.out.println("Springboot9EnableApplication3....");
25         return new Jedis("localhost", 6379);
26     }
27 
28 }

 

 

 

 


3 查看官方实现

 

 

 

 

 1 //
 2 // Source code recreated from a .class file by IntelliJ IDEA
 3 // (powered by Fernflower decompiler)
 4 //
 5 
 6 package org.springframework.boot.autoconfigure.data.redis;
 7 
 8 import java.net.UnknownHostException;
 9 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
10 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
11 import org.springframework.boot.context.properties.EnableConfigurationProperties;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.context.annotation.Configuration;
14 import org.springframework.context.annotation.Import;
15 import org.springframework.data.redis.connection.RedisConnectionFactory;
16 import org.springframework.data.redis.core.RedisOperations;
17 import org.springframework.data.redis.core.RedisTemplate;
18 import org.springframework.data.redis.core.StringRedisTemplate;
19 
20 @Configuration
21 @ConditionalOnClass({RedisOperations.class})
22 @EnableConfigurationProperties({RedisProperties.class})
23 @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
24 public class RedisAutoConfiguration {
25     public RedisAutoConfiguration() {
26     }
27 
28     @Bean
29     @ConditionalOnMissingBean(
30         name = {"redisTemplate"}
31     )
32     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
33         RedisTemplate<Object, Object> template = new RedisTemplate();
34         template.setConnectionFactory(redisConnectionFactory);
35         return template;
36     }
37 
38     @Bean
39     @ConditionalOnMissingBean
40     public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
41         StringRedisTemplate template = new StringRedisTemplate();
42         template.setConnectionFactory(redisConnectionFactory);
43         return template;
44     }
45 }
View Code
  1 //
  2 // Source code recreated from a .class file by IntelliJ IDEA
  3 // (powered by Fernflower decompiler)
  4 //
  5 
  6 package org.springframework.boot.autoconfigure.data.redis;
  7 
  8 import java.time.Duration;
  9 import java.util.List;
 10 import org.springframework.boot.context.properties.ConfigurationProperties;
 11 
 12 @ConfigurationProperties(
 13     prefix = "spring.redis"
 14 )
 15 public class RedisProperties {
 16     private int database = 0;
 17     private String url;
 18     private String host = "localhost";
 19     private String password;
 20     private int port = 6379;
 21     private boolean ssl;
 22     private Duration timeout;
 23     private RedisProperties.Sentinel sentinel;
 24     private RedisProperties.Cluster cluster;
 25     private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
 26     private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
 27 
 28     public RedisProperties() {
 29     }
 30 
 31     public int getDatabase() {
 32         return this.database;
 33     }
 34 
 35     public void setDatabase(int database) {
 36         this.database = database;
 37     }
 38 
 39     public String getUrl() {
 40         return this.url;
 41     }
 42 
 43     public void setUrl(String url) {
 44         this.url = url;
 45     }
 46 
 47     public String getHost() {
 48         return this.host;
 49     }
 50 
 51     public void setHost(String host) {
 52         this.host = host;
 53     }
 54 
 55     public String getPassword() {
 56         return this.password;
 57     }
 58 
 59     public void setPassword(String password) {
 60         this.password = password;
 61     }
 62 
 63     public int getPort() {
 64         return this.port;
 65     }
 66 
 67     public void setPort(int port) {
 68         this.port = port;
 69     }
 70 
 71     public boolean isSsl() {
 72         return this.ssl;
 73     }
 74 
 75     public void setSsl(boolean ssl) {
 76         this.ssl = ssl;
 77     }
 78 
 79     public void setTimeout(Duration timeout) {
 80         this.timeout = timeout;
 81     }
 82 
 83     public Duration getTimeout() {
 84         return this.timeout;
 85     }
 86 
 87     public RedisProperties.Sentinel getSentinel() {
 88         return this.sentinel;
 89     }
 90 
 91     public void setSentinel(RedisProperties.Sentinel sentinel) {
 92         this.sentinel = sentinel;
 93     }
 94 
 95     public RedisProperties.Cluster getCluster() {
 96         return this.cluster;
 97     }
 98 
 99     public void setCluster(RedisProperties.Cluster cluster) {
100         this.cluster = cluster;
101     }
102 
103     public RedisProperties.Jedis getJedis() {
104         return this.jedis;
105     }
106 
107     public RedisProperties.Lettuce getLettuce() {
108         return this.lettuce;
109     }
110 
111     public static class Lettuce {
112         private Duration shutdownTimeout = Duration.ofMillis(100L);
113         private RedisProperties.Pool pool;
114 
115         public Lettuce() {
116         }
117 
118         public Duration getShutdownTimeout() {
119             return this.shutdownTimeout;
120         }
121 
122         public void setShutdownTimeout(Duration shutdownTimeout) {
123             this.shutdownTimeout = shutdownTimeout;
124         }
125 
126         public RedisProperties.Pool getPool() {
127             return this.pool;
128         }
129 
130         public void setPool(RedisProperties.Pool pool) {
131             this.pool = pool;
132         }
133     }
134 
135     public static class Jedis {
136         private RedisProperties.Pool pool;
137 
138         public Jedis() {
139         }
140 
141         public RedisProperties.Pool getPool() {
142             return this.pool;
143         }
144 
145         public void setPool(RedisProperties.Pool pool) {
146             this.pool = pool;
147         }
148     }
149 
150     public static class Sentinel {
151         private String master;
152         private List<String> nodes;
153 
154         public Sentinel() {
155         }
156 
157         public String getMaster() {
158             return this.master;
159         }
160 
161         public void setMaster(String master) {
162             this.master = master;
163         }
164 
165         public List<String> getNodes() {
166             return this.nodes;
167         }
168 
169         public void setNodes(List<String> nodes) {
170             this.nodes = nodes;
171         }
172     }
173 
174     public static class Cluster {
175         private List<String> nodes;
176         private Integer maxRedirects;
177 
178         public Cluster() {
179         }
180 
181         public List<String> getNodes() {
182             return this.nodes;
183         }
184 
185         public void setNodes(List<String> nodes) {
186             this.nodes = nodes;
187         }
188 
189         public Integer getMaxRedirects() {
190             return this.maxRedirects;
191         }
192 
193         public void setMaxRedirects(Integer maxRedirects) {
194             this.maxRedirects = maxRedirects;
195         }
196     }
197 
198     public static class Pool {
199         private int maxIdle = 8;
200         private int minIdle = 0;
201         private int maxActive = 8;
202         private Duration maxWait = Duration.ofMillis(-1L);
203         private Duration timeBetweenEvictionRuns;
204 
205         public Pool() {
206         }
207 
208         public int getMaxIdle() {
209             return this.maxIdle;
210         }
211 
212         public void setMaxIdle(int maxIdle) {
213             this.maxIdle = maxIdle;
214         }
215 
216         public int getMinIdle() {
217             return this.minIdle;
218         }
219 
220         public void setMinIdle(int minIdle) {
221             this.minIdle = minIdle;
222         }
223 
224         public int getMaxActive() {
225             return this.maxActive;
226         }
227 
228         public void setMaxActive(int maxActive) {
229             this.maxActive = maxActive;
230         }
231 
232         public Duration getMaxWait() {
233             return this.maxWait;
234         }
235 
236         public void setMaxWait(Duration maxWait) {
237             this.maxWait = maxWait;
238         }
239 
240         public Duration getTimeBetweenEvictionRuns() {
241             return this.timeBetweenEvictionRuns;
242         }
243 
244         public void setTimeBetweenEvictionRuns(Duration timeBetweenEvictionRuns) {
245             this.timeBetweenEvictionRuns = timeBetweenEvictionRuns;
246         }
247     }
248 }
View Code

 

标签:SpringBoot2.6,return,SpringBoot,自定义,springframework,class,org,import,public
来源: https://www.cnblogs.com/yppah/p/15089914.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有