Spring @Autowired npe example:Why your Spring @Autowired component is null

时间:2022-07-24
本文章向大家介绍Spring @Autowired npe example:Why your Spring @Autowired component is null,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

问题描述:Controller方法误写成了private而导致500错误的问题。

找原因,找了大半天, 也翻了Spring MVC的源码。。。终于,灵机一动,看到了 private :

原因分析

接下来分析下不能写private的原因。

实际上SpringMVC本身对这个没有限制,会找到所有用户声明(ReflectionUtils.USER_DECLARED_METHODS)的方法。

但是,如果用到了切面(恰巧,Controller中经常用到切面,我们也用了。。。):

<aop:aspectj-autoproxy proxy-target-class="true" />

或者

@EnableAspectJAutoProxy(proxyTargetClass = true)

这种,那么实际上最后会使用Cglib做代理,而生成的代理类会代理所有能够代理的方法,换句话说,private方法肯定就没有了。

被 private 和 final 修饰的方法不会进入callback,如果进入不了callback,那么就进入不了被代理的目标对象。那么只能在proxy对象上执行private或final修饰的方法。而proxy对象是由cglib实例化的,里面没有spring注入的对象。因此,报错:空指针异常NPE。

这就难怪,同一个Controller里面的两个方法,请求过来,Controller的对象的地址居然是不同的,而且,可以看到,请求 private 方法的那次,Controller 对象是明显经过CGLib增强代理的:

解决方案

tomcat启动后使用 @Autowired注入bean成功,但调用方法的时候使用注入的bean对象都是null,最后发现我把这个方法的修饰符写成了private,所以请求的方法不能用private修饰,得使用public。

另外的解决办法,是通过实现一个ApplicationContext工具类进行手动注入。获取这个ApplicationContext对象,我们就能手动从Spring获取所需要的 bean :

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
@Component
public class ApplicationContextUtils implements ApplicationContextAware {
     
    private static ApplicationContext applicationContext;
 
    private ApplicationContextUtils(){}
     
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }
     
    public static <T> T getBean(Class<T> beanClass) {
        return applicationContext.getBean(beanClass);
    }
     
    public static <T> T getBean(String beanName, Class<T> beanClass) {
        return applicationContext.getBean(beanName, beanClass);
    }
 
}

在切面类中使用:

@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SysOperateLogAspect {
     
    private SysOperateLogService sysOperateLogService;
     
    public SysOperateLogAspect() {
        this.sysOperateLogService = ApplicationContextUtils.getBean(SysOperateLogService.class);
    }
}

相关源码

源码:org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator

protected Object createProxy(Class<?> beanClass, @Nullable String beanName, Object[] specificInterceptors, TargetSource targetSource) {
    // ....
    ProxyFactory proxyFactory = new ProxyFactory();
    //复制当前类的一些属性
    proxyFactory.copyFrom(this);
    // 如果在配置文件中配置的aop标签的属性proxy-target-class为false,
    if (!proxyFactory.isProxyTargetClass()) {
        // 是否需要代理当前类而不是代理接口,根据preserveTargetClass属性来判断Boolean.TRUE.equals(bd.getAttribute("preserveTargetClass")
        if (shouldProxyTargetClass(beanClass, beanName)) {
            proxyFactory.setProxyTargetClass(true);
        }
        else {
            // 如果代理的是接口,则添加代理接口
            evaluateProxyInterfaces(beanClass, proxyFactory);
        }
    }
    // 对增强器进行包装,有些增强是通过拦截器等方式来实现的,所以这里统一封装为 Advisor 进行处理
    Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
    // 加入增强器
    proxyFactory.addAdvisors(advisors);
    // 设置要代理的类
    proxyFactory.setTargetSource(targetSource);
    // 用户自定义代理
    customizeProxyFactory(proxyFactory);
    // 该属性用来控制代理工厂被配置以后,是否还允许修改通知,默认为false
    proxyFactory.setFrozen(this.freezeProxy);
    if (advisorsPreFiltered()) {
        proxyFactory.setPreFiltered(true);
    }
    // 创建代理
    return proxyFactory.getProxy(getProxyClassLoader());
}
 
// 添加接口代理
protected void evaluateProxyInterfaces(Class<?> beanClass, ProxyFactory proxyFactory) {
    Class<?>[] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass, getProxyClassLoader());
    boolean hasReasonableProxyInterface = false;
    //....
    if (hasReasonableProxyInterface) {
        for (Class<?> ifc : targetInterfaces) {
            proxyFactory.addInterface(ifc);
        }
    }
    else {
        proxyFactory.setProxyTargetClass(true);
    }
}
proxyFactory.getProxy(getProxyClassLoader());

通过这行代码我们可以看到有两个实现类分别是:JdkDynamicAopProxy 和 CglibAopProxy。追溯到CglibAopProxy 源码中:

    @Override
    public Object getProxy(ClassLoader classLoader) {
        if (logger.isDebugEnabled()) {
            logger.debug("Creating CGLIB proxy: target source is " + this.advised.getTargetSource());
        }
 
        try {
            Class<?> rootClass = this.advised.getTargetClass();
            Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");
 
            Class<?> proxySuperClass = rootClass;
            if (ClassUtils.isCglibProxyClass(rootClass)) {
                proxySuperClass = rootClass.getSuperclass();
                Class<?>[] additionalInterfaces = rootClass.getInterfaces();
                for (Class<?> additionalInterface : additionalInterfaces) {
                    this.advised.addInterface(additionalInterface);
                }
            }
 
            // Validate the class, writing log messages as necessary.
            validateClassIfNecessary(proxySuperClass, classLoader);
 
            // Configure CGLIB Enhancer...
            Enhancer enhancer = createEnhancer();
            if (classLoader != null) {
                enhancer.setClassLoader(classLoader);
                if (classLoader instanceof SmartClassLoader &&
                        ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
                    enhancer.setUseCache(false);
                }
            }
                        //设置需要创建子类的类
            enhancer.setSuperclass(proxySuperClass);
            enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
            enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
            enhancer.setStrategy(new UndeclaredThrowableStrategy(UndeclaredThrowableException.class));
 
            Callback[] callbacks = getCallbacks(rootClass);
            Class<?>[] types = new Class<?>[callbacks.length];
            for (int x = 0; x < types.length; x++) {
                types[x] = callbacks[x].getClass();
            }
            // fixedInterceptorMap only populated at this point, after getCallbacks call above
            enhancer.setCallbackFilter(new ProxyCallbackFilter(
                    this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
            enhancer.setCallbackTypes(types);
 
            // Generate the proxy class and create a proxy instance.
            return createProxyClassAndInstance(enhancer, callbacks);
        }
        catch (CodeGenerationException ex) {
            throw new AopConfigException("Could not generate CGLIB subclass of class [" +
                    this.advised.getTargetClass() + "]: " +
                    "Common causes of this problem include using a final class or a non-visible class",
                    ex);
        }
        catch (IllegalArgumentException ex) {
            throw new AopConfigException("Could not generate CGLIB subclass of class [" +
                    this.advised.getTargetClass() + "]: " +
                    "Common causes of this problem include using a final class or a non-visible class",
                    ex);
        }
        catch (Exception ex) {
            // TargetSource.getTarget() failed
            throw new AopConfigException("Unexpected AOP exception", ex);
        }
    }
 
    protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
        enhancer.setInterceptDuringConstruction(false);
        enhancer.setCallbacks(callbacks);
        return (this.constructorArgs != null ?
                enhancer.create(this.constructorArgTypes, this.constructorArgs) :
                enhancer.create());
    }

我们看到有一行代码 enhancer.setSuperclass(proxySuperClass); 这说明什么 cglib采用继承的方式通过生成子类的方式创建代理类;生成代理类前,设置了CallbackFilter,CallbackFilter允许我们在方法层设置回调(callback),根据我们对方法处理的需求设置不同的回调;callback才是真正执行我们目标对象方法的地方。


另外,也有其他的常见的 Spring @Autowired npe example, 特摘录如下:

The Spring framework makes heavy use of Inversion of Control (IoC) to let you inject classes without having to worry about their scope, lifetime or cleanup.

A common error people hit is when they autowire a class and when they try to call a method on it find that it is null and they get a NullPointerException. So why didn’t Spring auto-wire your class for you? Here’s two possible reasons:

YOU INSTANTIATED THE A CLASS MANUALLY Hi, 2005 called and asked for their code back. Yeah, OK, IoC is like the cool kid on the block and if you are using Spring then you need to be using it all the time. Here’s a code snippet of a Controller, Service and Repository that will result in a NullPointerException.

@Controller
public class Controller {

  @GetMapping("/example")
  public String example() {
    MyService my = new MyService();
    my.doStuff();
  }
}

@Service
public class MyService() {

  @Autowired
  MyRepository repo;

  public void doStuff() {
    repo.findByName( "steve" );
  }
}

 

@Repository
public interface MyRepository extends CrudRepository<My, Long> {

  List<My> findByName( String name );
}

This will throw a NullPointerException in the service class when it tries to access the MyRepository auto-wired Repository, not because there is anything wrong with the wiring of the Repository but because you instantiated MyService() manually with MyService my = new MyService(). To fix this auto-wire the Service as well:

@Controller
public class Controller {

  @Autowired
  MyService service;

  @GetMapping("/example")
  public String example() {
    service.doStuff();
  }
}

@Service
public class MyService() {

  @Autowired
  MyRepository repo;

  public void doStuff() {
    repo.findByName( "steve" );
  }
}

@Repository
public interface MyRepository extends CrudRepository<My, Long> {

  List<My> findByName( String name );
}

YOU FORGOT TO ANNOTATE A CLASS AS A COMPONENT OR ONE OF ITS DESCENDANTS

Spring uses component scanning to find the classes that it needs to auto-wire and insert into classes for IoC. Basically, Spring is going to scan the project’s classpath (or paths you specified), find all of the @Component classes and make them available for auto-wiring. So if you forget to annotate a class it will not be auto-wired and when you try and use it you will get a null and a NullPointerException.

@Service, @Repository and @Controller are all specializations of @Component, so any class you want to auto-wire needs to be annotated with one of them. So auto-wiring this will cause a null:

public class MyService {

  public void doStuff() {
  }
}
but this will work fine

@Service
public class MyService {

  public void doStuff() {
  }
}

参考资料

[1].https://blog.csdn.net/liruichuan/article/details/101367819 [2].https://github.com/chrylis/spring_autowired_npe_example [3].https://github.com/chrylis/spring_autowired_npe_example/tree/nonworking [4].https://www.moreofless.co.uk/spring-mvc-java-autowired-component-null-repository-service/