[已解决]@Autowired 失效、@Autowired 注入为null

一、问题

使用@Atuowried注入Bean失败,导致空指针异常。

二、大致背景

SpringBoot版本:2.6.14,基于SpringSecurity实现邮箱验证码登录功能。EmailCodeAuthenticationSuccessHandler为邮件验证码登录认证成功的Handler,实现认证成功后返回响应,响应体为登录用户名以及JWTUtil生成的token。EmailCodeAuthenticationSuccessHandler中使用@Atuowried将JWTUtil自动注入进来。

JWTUtil:

三、具体报错:

在生成token时,报了一个空指针异常:

(1)报错分析

在Spring Security的认证过程中,EmailcodeAuthenticationSuccessHandler类的onAuthenticationSuccess方法的第45行:data.put(“token”, jwtUtil.createToken(userId));,发生了一个NullPointerException(空指针异常)。

(2)问题定位

data是new出来的JSONObject(),只有可能是JWTUtil注入为null导致空指针异常。

(3)断点调试

bingo!确实是@Atuowried注入失败,导致jwtUtil为null,从而在调用createToken方法时出现空指针异常。

四、具体问题

为什么@Atuowried注入失败/@Atuowried注入为null

查各类博客总结如下:

(1)被注入的对象没有加载到Spring容器中

​ 缺少@Component之类的注解或者没有被Spring扫描到。

(2)自定义配置存在问题

​ 自定义的BeanFactory没有正确配置,导致Spring容器无法识别自定义的Bean。

(3)被注入的对象不是Spring加载

​ 通过反射或者热部署加载的类Spring无法根据注解自动注入。

(4)需要自动注入的对象存在被new出来的实例

​ 对象new实例化后,导致对象没有交给Spring容器管理,所以无法自动注入。一般是指引用某些框架,自定义了类继承某个接口,但是在这些框架中默认new过这个类,比如MVC拦截的HandlerInterceptor类。如果要new的这个类里有需要@autowired 自动注入的内容,则自动注入无效。

五、本问题的原因

在LoginServiceImpl中注入JWTUtil正常,问题出在接口AuthenticationFailureHandler上。可能是SpringSecurity 通过new创建过 EmailcodeAuthenticationSuccessHandler的实例,导致其无法实现自动注入。

六、解决方案

确实需要在这个new 的类去注入某些类,但是用@Autowired 又注入为null,这时候就需要去实现ApplicationContextAware接口,拿到IOC容器,实现手动获取Bean。

(1)具体代码:

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

/**

* @Description 一个类实现了ApplicationContextAware接口后,就可以获得ApplicationContext中的所有bean

* 用于解决某些类因为有被new出来的实例导致@Autowired失效的问题

* @Author wxp

* @Date 2024/7/9 12:47

*/

@Component

public class BeanUtils implements ApplicationContextAware {

protected static ApplicationContext applicationContext;

/**

* 实现ApplicationContextAware接口的回调方法,设置上下文环境

* @param arg spring上下文对象

* @throws BeansException 抛出spring异常

*/

@Override

public void setApplicationContext(ApplicationContext arg) throws BeansException {

if (applicationContext == null) {

applicationContext = arg;

}

}

/**

* 获取spring上下文对象

* @return 上下文对象

*/

public static ApplicationContext getContext() {

return context;

}

/**

* 根据beanName获取bean

* @param beanName bean的名称

* @return bean对象

*/

public Object getBean(String beanName) {

return context.getBean(beanName);

}

/**

* 根据beanName和类型获取bean

* @param beanName bean名称

* @param clazz bean的Class类型

* @param bean的类型

* @return bean对象

*/

public T getBean(String beanName, Class clazz) {

return context.getBean(beanName, clazz);

}

/**

* 根据类型获取bean

* @param clazz bean的Class类型

* @param bean的类型

* @return bean对象

*/

public T getBean(Class clazz) {

return context.getBean(clazz);

}

}

(2)具体使用:

JWTUtil jwtUtil = BeanUtils.getBean(JWTUtil.class);