All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xin.xihc.jba.SpringContextUtil Maven / Gradle / Ivy

There is a newer version: 1.8.12
Show newest version
/**
 *
 */
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
 */
@Order(0)
@Component
public class SpringContextUtil implements ApplicationContextAware {

	private static ApplicationContext applicationContext; // Spring应用上下文环境

	/*
	 * 实现了ApplicationContextAware 接口,必须实现该方法;
	 * 通过传递applicationContext参数初始化成员变量applicationContext
	 */
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.applicationContext = applicationContext;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 根据名称获取Bean
	 *
	 * @param name
	 * @return
	 */
	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}

	/**
	 * 根据类型获取Bean
	 *
	 * @param clazz
	 * @param 
	 * @return
	 */
	public static  T getBean(Class clazz) {
		return applicationContext.getBean(clazz);
	}

	/**
	 * 获取Bean,根据名称&类型
	 *
	 * @param name
	 * @param clazz
	 * @param 
	 * @return
	 */
	public static  T getBean(String name, Class clazz) {
		return applicationContext.getBean(name, clazz);
	}

	/**
	 * 获取带有注解的Bean
	 *
	 * @param annotationType
	 * @return
	 */
	public static Map getBeansWithAnnotation(Class annotationType) {
		return applicationContext.getBeansWithAnnotation(annotationType);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy