在线程中获取不到 bean 对象,只能通过获取上下文的方式来获取
实现 ApplicationContextAware 接口
package com.example.nettydemo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author dong
* @date 2022/4/6
* @AapiNote
*/
@Component
@Slf4j
public class GetSpringBeanUtil implements ApplicationContextAware {
// 定义静态ApplicationContext
private static ApplicationContext applicationContext = null;
/**
* 重写接口的方法,该方法的参数为框架自动加载的IOC容器对象
* 该方法在启动项目的时候会自动执行,前提是该类上有IOC相关注解,例如@Component
* @param applicationContext ioc容器
* @throws BeansException e
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// 将框架加载的ioc赋值给全局静态ioc
GetSpringBeanUtil.applicationContext = applicationContext;
log.info("==================ApplicationContext加载成功==================");
}
// 获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
// 通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
// 通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
// 通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
使用案列
// 这里是通过 bean 的名称 获取到bean
BlacklistService blacklistService = (BlacklistService) GetSpringBeanUtil.getBean("blacklistService");
@Service(value = "blacklistService")
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, Blacklist> implements BlacklistService {