一、前言:
前不久在弄 spring security + jjwt 认证过滤器的时候,需要给 UsernamePasswordAuthenticationToken
类的构造方法传入 Collection<? extends GrantedAuthority> authorities
作为后续的 身份(role)验证。
但是 我通过 @Autowired
自动注入的 bean 无法生效,一直显示为 null,并抛出空指针异常。
二、分析:
首先检查扫描包路径没问题。最终确定容器加载顺序引发的问题。各个元素的执行顺序是这样的,context-param–>listener–>filter–>servlet 可以看出在Spring MVC 的dispatcherservlet初始化之前过滤器就已经加载好了,所以注入的是null。
三、解决办法:
SpringContextUtils 上下文工具类代码如下:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
*@ClassName: SpringContextUtils
*@Description: 实现 ApplicationContextAware 接口创建上下文工具类 SpringContextUtils
*@Params:
*@Return:
*@Author xxw
*@Date 2022/2/28
*/
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext=applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
if (applicationContext == null){
return null;
}
return (T)applicationContext.getBean(name);
}
}
使用如下:
四、深入了解 ApplicationContext-Spring上下文:
1、Spring容器
Spring容器最基本的接口就是BeanFactor。BeanFactory负责配置、创建、管理Bean,他有一个子接口:ApplicationContext,因此也称之为Spring上下文。Spring容器负责管理Bean与Bean之间的依赖关系。
BeanFactory接口包含以下几个基本方法:
调用者只需使用getBean()方法即可获得指定Bean的引用,无须关心Bean的实例化过程。即Bean实例的创建过程完全透明。
在使用BeanFactory接口时,我们一般都是使用这个实现类:org.springframework.beans.factory.xml.XmlBeanFactory。然而ApplicationContext 作为 BeanFactory的子接口,使用它作为Spring容器会更加方便。它的实现类有:FileSystemXmlApplicationContext、ClassPathXmlApplicationContext、AnnotationConfigApplicationContext
。
创建Spring容器实例时,必须提供Spring容器管理的Bean的详细配置信息。Spring的配置信息通常采用xml配置文件来设置,因此,创建BeanFactory实例时,应该提供XML配置文件作为参数。
2、实现ApplicationContextAware接口获取Spring容器中的 bean
简单的介绍了Spring容器。在Spring中我们可以使用Spring容器中getBean()方法来获取Spring容器中的Bean实例。在这样的访问模式下,程序中总是持有Spring容器的引用。但是在实际的应用中,Spring容器通常是采用声明式方式配置产生:开发者只要在web.xml文件中配置一个Listener,该Listener将会负责初始化Spring容器。在这种情况下,容器中Bean处于容器管理下,无须主动访问容器,只需要接受容器的注入管理即可。同时Bean实例的依赖关系通常也是由容器自动注入,无须Bean实例主动请求。 Spring容器中Bean通常不会需要访问容器中其他的Bean—采用依赖注入,让Spring把被依赖的Bean注入到依赖的Bean中即可。
ApplicationContextAware
通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware
接口中的setApplicationContext
方法。 我们在ApplicationContextAware的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。
实现ApplicationContextAware接口:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class RpcServer implements ApplicationContextAware{
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
context = applicationContext;
}
// 获得applicationContext
public static ApplicationContext getApplicationContext() {
return context;
}
//获取Bean
public static <T> T getBean(Class<T> requiredType){
//assertContextInjected();
return (T) getApplicationContext().getBean(requiredType);
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name){
assertContextInjected();
return (T) getApplicationContext().getBean(name);
}
}