起因:SpringBoot我是越用越喜欢,但当SpringBoot出了问题的时候,我却无从下手,因为封装实在是太高度化了。然后之前有一个需求,使用SpringBoot提供的StringRedisTemplate,我想定制里面几个属性。如下面代码。

private boolean enableTransactionSupport = false; 
private boolean exposeConnection = false; 
private boolean initialized = false; 
private boolean enableDefaultSerializer = true; 
private @Nullable RedisSerializer<?> defaultSerializer; 
private @Nullable ClassLoader classLoader; 

@SuppressWarnings("rawtypes") private @Nullable RedisSerializer keySerializer = null; 
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer valueSerializer = null; 
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashKeySerializer = null; 
@SuppressWarnings("rawtypes") private @Nullable RedisSerializer hashValueSerializer = null; 
private RedisSerializer<String> stringSerializer = new StringRedisSerializer(); 

private @Nullable ScriptExecutor<K> scriptExecutor; 

// cache singleton objects (where possible) 
private @Nullable ValueOperations<K, V> valueOps; 
private @Nullable ListOperations<K, V> listOps; 
private @Nullable SetOperations<K, V> setOps; 
private @Nullable ZSetOperations<K, V> zSetOps; 
private @Nullable GeoOperations<K, V> geoOps; 
private @Nullable HyperLogLogOperations<K, V> hllOps;

但我每次使用都是直接autowire注入进去的,然后注入进去并不能set & get 来修改属性,这高度封装就产生了一个问题。像之前用Spring,在xml文件配置一下即可,但SpringBoot呢?

方法一:

以最常见的DataSource数据库为例。一般注入DataSource直接在application.properties配置一下数据源即可以使用,返回的为SpringBoot默认的数据源,号称史上最快的HikariDataSource。但假设我想修改里面的配置如何?比如为连接池起一个名字?

package com.config; 
  
import javax.sql.DataSource; 
  
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
  
import com.zaxxer.hikari.HikariConfig; 
import com.zaxxer.hikari.HikariDataSource; 
 
@Configuration 
public class DataSourceConfig { 
     
    @Bean 
    public DataSource getDataSource() { 
         
        HikariConfig config = new HikariConfig(); 
        config.setUsername("name"); 
        config.setPassword("pass"); 
        config.setJdbcUrl("url"); 
        //测试 
        config.setPoolName("do you see me"); 
         
        return new HikariDataSource(config); 
    } 
}

直接新建立一个DataSourceConfig类,然后加上@Configuration注解。最后再写一个方法,方法名亲测可以随便。加上@Bean注解,然后SpringBoot就会自动加载这个配置类了。你就可以定制自己喜欢的属性了。

ps。这里有一个疑问,SpringBoot如何知道这个Bean是配置的数据源呢?方法名亲测可以胡乱改的,难道根据返回的值?请大神指教。

方法二:

假设你想在配置文件里面配置好变量,然后在类中直接使用已经定义好的变量。这样比较好维护。

package com.redis; 
  
import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.context.annotation.Bean; 
import org.springframework.stereotype.Component; 
  
import redis.clients.jedis.JedisPool; 
import redis.clients.jedis.JedisPoolConfig; 
 
@Component 
@ConfigurationProperties(prefix = "spring.redis") 
public class RedisConfig { 
 
    private String host; 
    private int port; 
    private String password; 
     
    /** 
     * timeout  3000毫秒 
     * @return 
     * 2018年5月21日 
     */ 
    @Bean   
    public JedisPool getJedisPool() {   
        JedisPoolConfig config = new JedisPoolConfig();  
        config.setMaxTotal(1000); 
        config.setMaxIdle(1000); 
        config.setMaxWaitMillis(3000); 
        JedisPool pool = new JedisPool(config, host, port, 3000 ,password); 
        return pool;   
    }   
     
}

注解变为@ConfigurationProperties,定义好直接使用即可,方便得很。

 

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!