【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,定义好直接使用即可,方便得很。

猜你喜欢

转载自www.cnblogs.com/wenbochang/p/9090474.html
今日推荐