xin.xihc.jba.SpringContextUtil Maven / Gradle / Ivy
/**
*
*/
package xin.xihc.jba;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.util.Map;
/**
* @author Leo.Xi
* @date 2018年1月24日
* @since 0.0.1
*/
@Order(0)
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 实现了ApplicationContextAware 接口,必须实现该方法;
* 通过传递applicationContext参数初始化成员变量applicationContext
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* 根据名称获取Bean
*
* @param name bean的名称
* @return
*/
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
/**
* 根据类型获取Bean
*
* @param clazz bean的类型
* @return bean的实例
*/
public static T getBean(Class clazz) {
return applicationContext.getBean(clazz);
}
/**
* 获取Bean,根据名称&类型
*
* @param name bean名称
* @param clazz bean的类型
* @return bean的实例
*/
public static T getBean(String name, Class clazz) {
return applicationContext.getBean(name, clazz);
}
/**
* 获取带有注解的Bean
*
* @param annotationType 注解类型
* @return
*/
public static Map getBeansWithAnnotation(Class extends Annotation> annotationType) {
return applicationContext.getBeansWithAnnotation(annotationType);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy