
com.diboot.framework.utils.ContextHelper Maven / Gradle / Ivy
The newest version!
package com.diboot.framework.utils;
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;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Spring上下文帮助类
* @author [email protected]
* @version 2017/8/12
*
*/
@Component
@Lazy(false)
public class ContextHelper implements ApplicationContextAware {
/***
* ApplicationContext上下文
*/
private static ApplicationContext APPLICATION_CONTEXT = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(APPLICATION_CONTEXT == null){
APPLICATION_CONTEXT = applicationContext;
}
}
/***
* 获取ApplicationContext上下文
*/
public static ApplicationContext getApplicationContext() {
return APPLICATION_CONTEXT;
}
/***
* 获取Bean实例
* @param beanId
* @return
*/
public static Object getBean(String beanId){
ApplicationContext wac = getApplicationContext();
if(wac != null){
return wac.getBean(beanId);
}
return null;
}
/***
* 获取bean实例
* @param type
* @param
* @return
*/
public static List getBeans(Class type){
// 获取所有的定时任务实现类
List beanList = new ArrayList<>();
ApplicationContext wac = getApplicationContext();
Map map = wac.getBeansOfType(type);
if(V.notEmpty(map)){
for(Map.Entry entry : map.entrySet()){
beanList.add(entry.getValue());
}
}
return beanList;
}
/***
* 根据注解获取beans
* @param annotationType
* @return
*/
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy