【SpringBoot系列4】SpringBoot定制自己的bean


起因:SpringBoot我是越用越喜歡,但當SpringBoot出了問題的時候,我卻無從下手,因為封裝實在是太高度化了。然后之前有一個需求,使用SpringBoot提供的StringRedisTemplate,我想定制里面幾個屬性。如下面代碼。

 1    private boolean enableTransactionSupport = false;
 2     private boolean exposeConnection = false;
 3     private boolean initialized = false;
 4     private boolean enableDefaultSerializer = true;
 5     private @Nullable RedisSerializer<?> defaultSerializer;
 6     private @Nullable ClassLoader classLoader;
 7 
 8     @SuppressWarnings("rawtypes") private @Nullable RedisSerializer keySerializer = null;
 9     @SuppressWarnings("rawtypes") private @Nullable RedisSerializer valueSerializer = null;
10     @SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashKeySerializer = null;
11     @SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashValueSerializer = null;
12     private RedisSerializer<String> stringSerializer = new StringRedisSerializer();
13 
14     private @Nullable ScriptExecutor<K> scriptExecutor;
15 
16     // cache singleton objects (where possible)
17     private @Nullable ValueOperations<K, V> valueOps;
18     private @Nullable ListOperations<K, V> listOps;
19     private @Nullable SetOperations<K, V> setOps;
20     private @Nullable ZSetOperations<K, V> zSetOps;
21     private @Nullable GeoOperations<K, V> geoOps;
22     private @Nullable HyperLogLogOperations<K, V> hllOps;

 

但我每次使用都是直接autowire注入進去的,然后注入進去並不能set & get 來修改屬性,這高度封裝就產生了一個問題。像之前用Spring,在xml文件配置一下即可,但SpringBoot呢?

方法一:

以最常見的DataSource數據庫為例。一般注入DataSource直接在application.properties配置一下數據源即可以使用,返回的為SpringBoot默認的數據源,號稱史上最快的HikariDataSource。但假設我想修改里面的配置如何?比如為連接池起一個名字?

 1 package com.config;
 2 
 3 import javax.sql.DataSource;
 4 
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 
 8 import com.zaxxer.hikari.HikariConfig;
 9 import com.zaxxer.hikari.HikariDataSource;
10 
11 @Configuration
12 public class DataSourceConfig {
13     
14     @Bean
15     public DataSource getDataSource() {
16         
17         HikariConfig config = new HikariConfig();
18         config.setUsername("name");
19         config.setPassword("pass");
20         config.setJdbcUrl("url");
21         //測試
22         config.setPoolName("do you see me");
23         
24         return new HikariDataSource(config);
25     }
26 }

直接新建立一個DataSourceConfig類,然后加上@Configuration注解。最后再寫一個方法,方法名親測可以隨便。加上@Bean注解,然后SpringBoot就會自動加載這個配置類了。你就可以定制自己喜歡的屬性了。

ps。這里有一個疑問,SpringBoot如何知道這個Bean是配置的數據源呢?方法名親測可以胡亂改的,難道根據返回的值?請大神指教。

方法二:

假設你想在配置文件里面配置好變量,然后在類中直接使用已經定義好的變量。這樣比較好維護。

 1 package com.redis;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.stereotype.Component;
 6 
 7 import redis.clients.jedis.JedisPool;
 8 import redis.clients.jedis.JedisPoolConfig;
 9 
10 @Component
11 @ConfigurationProperties(prefix = "spring.redis")
12 public class RedisConfig {
13 
14     private String host;
15     private int port;
16     private String password;
17     
18     /**
19      * timeout  3000毫秒
20      * @return
21      * 2018年5月21日
22      */
23     @Bean  
24     public JedisPool getJedisPool() {  
25         JedisPoolConfig config = new JedisPoolConfig(); 
26         config.setMaxTotal(1000);
27         config.setMaxIdle(1000);
28         config.setMaxWaitMillis(3000);
29         JedisPool pool = new JedisPool(config, host, port, 3000 ,password);
30         return pool;  
31     }  
32     
33 }

注解變為@ConfigurationProperties,定義好直接使用即可,方便得很。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM