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> T getBean(String beanName) {
assertApplicationContext();
return (T) applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> 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();
}