栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

@EnableConfigurationProperties

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@EnableConfigurationProperties

@EnableConfigurationProperties

CacheProperties类使用了@ConfigurationProperties注解,可以从配置文件加载配置参数值赋类里各个成员变量,@EnableConfigurationProperties
根据字面意思判断就是将使用@ConfigurationProperties注解的类注入到Spring IOC容器里,交给Spring管理

@EnableConfigurationProperties(value = CacheProperties.class)
–相当于–>
@Component@ConfigurationProperties(prefix = “spring.cache”)

```java

//开启属性绑定配置功能,CacheProperties类只用了 @ConfigurationProperties注解,没有用@Component将该类注册到容器里,这样无法获得配置信息转化成的bean
//@EnableConfigurationProperties注解,根据意思-开启配置属性,就是使@ConfigurationProperties注解生效,把使用该注解的类进行一次注入
@EnableConfigurationProperties(value = CacheProperties.class)
@Configuration  //配置类
@EnableCaching  //开启注解
public class MyCacheConfig {


    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer(Object.class)));
        config= config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        
        //指定序列化-Jackson
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        //指定序列化-fastjson
        //config= config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));

        //从所有配置中取出redis的配置
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        //将配置文件中所有的配置都生效(直接从源码里面拷 RedisCacheConfiguration)
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;

    }

}

 
@ConfigurationProperties(
    prefix = "spring.cache"
)
public class CacheProperties {
    private CacheType type;
    private List cacheNames = new ArrayList();
    private final CacheProperties.Caffeine caffeine = new CacheProperties.Caffeine();
    private final CacheProperties.Couchbase couchbase = new CacheProperties.Couchbase();
    private final CacheProperties.EhCache ehcache = new CacheProperties.EhCache();
    private final CacheProperties.Infinispan infinispan = new CacheProperties.Infinispan();
    private final CacheProperties.JCache jcache = new CacheProperties.JCache();
    private final CacheProperties.Redis redis = new CacheProperties.Redis();

    public CacheProperties() {
    }

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号