cn.antcore.security.helper.ContextUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boot-starter-security Show documentation
Show all versions of spring-boot-starter-security Show documentation
This is spring-boot-starter-security.
The newest version!
package cn.antcore.security.helper;
import org.springframework.beans.BeansException;
import org.springframework.context.*;
import org.springframework.stereotype.Component;
import java.util.Collection;
/**
* Spring上下文工具类
*
* Created by Hong.
* 2021/3/24
**/
@Component
public class ContextUtils implements ApplicationContextAware, ApplicationEventPublisherAware {
/** Spring 上下文 **/
private static ApplicationContext applicationContext;
private static ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ContextUtils.applicationContext = applicationContext;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
ContextUtils.applicationEventPublisher = applicationEventPublisher;
}
/**
* 获取上下文
* @return Spring上下文
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取Bean
* @param tClass Bean class
* @param
* @return Bean实例
*/
public static T getBean(Class tClass) {
return applicationContext.getBean(tClass);
}
/**
* 获取Bean集合
* @param tClass Bean class
* @param
* @return Bean实例集合
*/
public static Collection getBeans(Class tClass) {
return applicationContext.getBeansOfType(tClass).values();
}
/**
* 发布事件
* @param event
*/
public static void publishEvent(ApplicationEvent event) {
applicationEventPublisher.publishEvent(event);
}
}