org.macrocloud.kernel.toolkit.utils.SpringUtil Maven / Gradle / Ivy
package org.macrocloud.kernel.toolkit.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.lang.Nullable;
/**
* spring 工具类.
*/
/** The Constant log. */
@Slf4j
public class SpringUtil implements ApplicationContextAware {
/** The context. */
private static ApplicationContext context;
/**
* Title: setApplicationContext
* Description:
* @param context
* @throws BeansException
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
SpringUtil.context = context;
}
/**
* 获取bean.
*
* @param 泛型
* @param clazz class类
* @return T
*/
public static T getBean(Class clazz) {
if (clazz == null) {
return null;
}
return context.getBean(clazz);
}
/**
* 获取bean.
*
* @param 泛型
* @param beanId beanId
* @return T
*/
public static T getBean(String beanId) {
if (beanId == null) {
return null;
}
return (T) context.getBean(beanId);
}
/**
* 获取bean.
*
* @param 泛型
* @param beanName bean名称
* @param clazz class类
* @return T
*/
public static T getBean(String beanName, Class clazz) {
if (null == beanName || "".equals(beanName.trim())) {
return null;
}
if (clazz == null) {
return null;
}
return (T) context.getBean(beanName, clazz);
}
/**
* 获取 ApplicationContext.
*
* @return ApplicationContext
*/
public static ApplicationContext getContext() {
if (context == null) {
return null;
}
return context;
}
/**
* 发布事件.
*
* @param event 事件
*/
public static void publishEvent(ApplicationEvent event) {
if (context == null) {
return;
}
try {
context.publishEvent(event);
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy