通过工具类获取spring容器中的bean

package com.springboot.app;

import com.springboot.testdemo.dao.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * FileName: AppContext
 * Description: 通过工具类获取spring容器中的bean
 */
@Component
@SuppressWarnings("static-access")
public class AppContext implements ApplicationContextAware
{

    private static Logger logger = LoggerFactory.getLogger(AppContext.class);

    private static AppContext instance;

    private static ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        instance=this;
    }

    public static ApplicationContext getAppContext(){
        return null!=instance.applicationContext ?instance.applicationContext:null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AppContext.applicationContext=applicationContext;
    }

    public static String getProperty(String key){
        return instance.applicationContext.getBean(Environment.class).getProperty(key);
    }

    private static Object getBean(String beanName){
        return null!=instance.applicationContext?instance.applicationContext.getBean(beanName) : null;
    }

    public static <T> T getBean(Class<T> cla){
        return null!=instance.applicationContext ? instance.applicationContext.getBean(cla) : null;
    }

    public static <T> T getBean(String beanName,Class<T> cla){
        return null!=instance.applicationContext ? instance.applicationContext.getBean(beanName,cla) : null;
    }

    @SuppressWarnings("unchecked")
    public static RedisTemplate<String,Object> getRedisTemplate(){
        return null!=instance.applicationContext? instance.applicationContext.getBean("redisTemplate",RedisTemplate.class) : null;
    }

    public static TestService detemineServiceByTopicCode(String topicCode){
        return null!=instance.applicationContext ? instance.applicationContext.getBean(TopicEnum.getServiceNameByTopicCode(topicCode),TestService.class) : null;
    }
}

缓存配置

package com.springboot.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.cache.Caching;
import javax.cache.spi.CachingProvider;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;

/**
 * FileName: CacheConfig
 * Description:缓存配置
 */
@SpringBootApplication
@EnableCaching
public class CacheConfig<K,V>
{
    @Autowired
    private RedisConnectionFactory factory;

    @Bean("redisTemplate")
    public RedisTemplate<String,Object> redisTemplate(){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean("redisCacheManager")
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate){
        return RedisCacheManager.create(factory);
    }

    @Bean("jCacheCacheManager")
    public JCacheCacheManager jCacheCacheManager() throws URISyntaxException {
        CachingProvider provider = Caching.getCachingProvider();
        JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
        provider.getCacheManager(getClass().getResource("/config/ehcache.xml").toURI(),getClass().getClassLoader());
        return jCacheCacheManager;
    }

    @Bean("cacheManager")
    @Primary
    public CacheManager initAppCacheManager(RedisCacheManager redisCacheManager,JCacheCacheManager jCacheCacheManager){
        MultipleCacheManager cacheManager = new MultipleCacheManager();
        cacheManager.setRedisCacheManager(redisCacheManager);
        cacheManager.setjCacheCacheManager(jCacheCacheManager);
        return cacheManager;
    }
}
class MultipleCacheManager implements CacheManager{
    private RedisCacheManager redisCacheManager;
    private JCacheCacheManager jCacheCacheManager;

    private final static String redisPrefix="redis";
    @Override
    public Cache getCache(String name) {
        if (name.startsWith(redisPrefix)) {
            return redisCacheManager.getCache(name);
        }else {
            return jCacheCacheManager.getCache(name);
        }
    }

    @Override
    public Collection<String> getCacheNames() {
        Collection<String> cacheNames = new ArrayList<>();
        if (redisCacheManager != null) {
            cacheNames.addAll(redisCacheManager.getCacheNames());
        }
        if (jCacheCacheManager != null) {
            cacheNames.addAll(jCacheCacheManager.getCacheNames());
        }
        return cacheNames;
    }

    public RedisCacheManager getRedisCacheManager() {
        return redisCacheManager;
    }

    public void setRedisCacheManager(RedisCacheManager redisCacheManager) {
        this.redisCacheManager = redisCacheManager;
    }

    public JCacheCacheManager getjCacheCacheManager() {
        return jCacheCacheManager;
    }

    public void setjCacheCacheManager(JCacheCacheManager jCacheCacheManager) {
        this.jCacheCacheManager = jCacheCacheManager;
    }
}

枚举设置

package com.springboot.app;

public enum TopicEnum
{
    TEST1("T1","实现类1","testServiceImpl1"),
    Tset2("T2","实现类2","testServiceImpl2");

    private String topicCode;
    private String topicName;
    private String serviceName;

    TopicEnum(String topicCode, String topicName, String serviceName) {
        this.topicCode = topicCode;
        this.topicName = topicName;
        this.serviceName = serviceName;
    }

    public static TopicEnum getEnumByCode(String code){
        for (TopicEnum top : TopicEnum.values()) {
            if (code.equals(top.getTopicCode())) {
                return top;
            }
        }
        return null;
    }

    public static String getServiceNameByTopicCode(String code){
        for (TopicEnum top : TopicEnum.values()) {
            if (code.equals(top.getTopicCode())) {
                return top.getServiceName();
            }
        }
        return null;
    }

    public String getTopicCode() {
        return topicCode;
    }

    public void setTopicCode(String topicCode) {
        this.topicCode = topicCode;
    }

    public String getTopicName() {
        return topicName;
    }

    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值