一、前言:
前不久在弄 spring security + jjwt 认证过滤器的时候,需要给 UsernamePasswordAuthenticationToken
类的构造方法传入 Collection<? extends GrantedAuthority> authorities
作为后续的 身份(role)验证。
UsernamePasswordAuthenticationToken 类提供了两个重载的构造方法:
UsernamePasswordAuthenticationToken(Object principal, Object credentials)
=> 生成无需身份验证的令牌
UsernamePasswordAuthenticationToken(Object principal, Object credentials,Collection<? extends GrantedAuthority> authorities)
=> 生成可信的未经身份验证的真实身份验证令牌
但是 我通过 @Autowired
自动注入的 bean 无法生效,一直显示为 null,并抛出空指针异常。
二、分析:
首先检查扫描包路径没问题。最终确定容器加载顺序引发的问题。各个元素的执行顺序是这样的,context-param–>listener–>filter–>servlet 可以看出在Spring MVC 的dispatcherservlet初始化之前过滤器就已经加载好了,所以注入的是null。
总结一句话:filter过滤器是servlet规范中定义的,并不归spring容器管理,也无法直接注入spring中的bean
三、解决办法:
在做开发的时候,并不是说在每一个地方都能将Bean注入到我们想要的地方去,比如在Utils使用到dao, 过滤器中使用 到 service,我们就不能直接注入了,这个时候就是我们需要封装 SpringContextUtils 的时候了,而 ApplicationContextAware 就起了关键性的作用。
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上下文:
①、Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口。他们都可代表Spring容器,Spring容器是生成Bean实例的工厂,并且管理容器中的Bean。
②、Bean是Spring管理的基本单位,在基于Spring的Java EE应用中,所有的组件都被当成Bean处理,包括数据源、Hibernate的SessionFactory、事务管理器等。在Spring中,Bean的是一个非常广义的概念,任何的Java对象、Java组件都被当成Bean处理。
③、而且应用中的所有组件,都处于Spring的管理下,都被Spring以Bean的方式管理,Spring负责创建Bean实例,并管理他们的生命周期。Bean在Spring容器中运行,无须感受Spring容器的存在,一样可以接受Spring的依赖注入,包括Bean属性的注入,协作者的注入、依赖关系的注入等。
④、Spring容器负责创建Bean实例,所以需要知道每个Bean的实现类,Java程序面向接口编程,无须关心Bean实例的实现类;但是Spring容器必须能够精确知道每个Bean实例的实现类,因此Spring配置文件必须精确配置Bean实例的实现类。
1、Spring容器
Spring容器最基本的接口就是BeanFactor。BeanFactory负责配置、创建、管理Bean,他有一个子接口:ApplicationContext,因此也称之为Spring上下文。Spring容器负责管理Bean与Bean之间的依赖关系。
BeanFactory接口包含以下几个基本方法:
Ø Boolean containBean(String name):判断Spring容器是否包含id为name的Bean实例。
Ø getBean(Class requiredTypr):获取Spring容器中属于requiredType类型的唯一的Bean实例。
Ø Object getBean(String name):返回Sprin容器中id为name的Bean实例。
Ø T getBean(String name,Class requiredType):返回容器中id为name,并且类型为requiredType的Bean
Ø Class <?> getType(String name):返回容器中指定Bean实例的类型。
调用者只需使用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);
}
}