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

SpringContextHolder实现ApplicationContextAware接口获取spring管理的上下文信息

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

SpringContextHolder实现ApplicationContextAware接口获取spring管理的上下文信息

package com.kindo.uas.common.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

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

    public static ApplicationContext getApplicationContext() {
        assertApplicationContext();
        return applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static  T getBean(String beanName) {
        assertApplicationContext();
        return (T) applicationContext.getBean(beanName);
    }

    public static  T getBean(Class requiredType) {
        assertApplicationContext();
        return applicationContext.getBean(requiredType);
    }

    private static void assertApplicationContext() {
        if (SpringContextHolder.applicationContext == null) {
            throw new RuntimeException("applicaitonContext属性为null,请检查是否注入了SpringContextHolder!");
        }
    }
}

这个是bean服务类:

@Service("zjjxASynthService")
public class IZjjxASynthServiceImpl implements IZjjxASynthService {
	//
}

获取对象信息:

public void receiveMessage2(String jsonMsg){
        ReentrantLock lock=new ReentrantLock();
        lock.lock();
        try{
            log.info("[开始消费REDIS消息队列数据,同步病案首页数据...]"+jsonMsg);
            Object  obj= SpringContextHolder.getBean("zjjxASynthService");
            Class z=obj.getClass();
            Method m = z.getDeclaredMethod("implement");
            m.invoke(obj);
            System.out.println(jsonMsg);
            log.info("[消费REDIS消息队列数据,同步病案首页数据成功.]"+jsonMsg);
        }catch (Exception e) {
            log.error("[消费REDIS消息队列数据同步病案首页数据失败,失败信息:{}]", e);
        }finally {
            lock.unlock();
        }
        latch.countDown();
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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