问题描述
环境:spring mvc 4.3.17
现象:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
描述:很早以前的老代码,以前同事没注意,某个Controller里面的RequestMapping方法用了private修饰(因为Spring是反射调用所以也能接受到请求)。最近,另一位同事针对这个Controller加了一段aop。测试环境自测就发现报NPE异常了。
代码现场:我模拟问题代码写了一段,首先是controller类文件,helloPri是private修饰的私有方法
@RestController
@RequestMapping("/focuse")
public class DemoController {
@Resource
private DemoPrinter demoPrinter;
@RequestMapping("hello")
public String hello(@RequestParam("text") String text) {
return demoPrinter.print(text);
}
@RequestMapping("hello_pri")
private String helloPri(@RequestParam("text") String text) {
return demoPrinter.print(text);
}
}
其次是aop的配置,就是BeanNameAutoProxyCreator,(只是模拟问题代码现场,配置尽量简单了)
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>demoController</value>
</list>
</property>
</bean>
请求/focuse/hello_pri抛异常,请求/focuse/hello正常

原因分析
这个问题不容易看出来(火眼金睛能注意到前面的private),我们是debug断点发现,当前对象并不是DemoContrller而是CGLIB生成的类,觉得有点奇怪。再对比一下没问题的请求,它的对象就是DemoController。
有问题的请求:
没问题的请求:
为什么会出现如此情况? 那得看看spring容器在初始化bean的过程中何时初始化aop,生成动态代理类;又是何时自动设置依赖属性的。
Spring容器设置Bean的依赖属性、aop初始化都是通过BeanPostProcessor机制实现的。@Resource是通过CommonAnnotationBeanPostProcessor实现的,@Autowired、@Value是通过AutowiredAnnotationBeanPostProcessor实现的,BeanNameAutoProxyCreator本身就是BeanPostProcessor,这三个都是特殊的BeanPostProcessor(InstantiationAwareBeanPostProcessor)。
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
......
PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException;
......
}
如上,InstantiationAwareBeanPostProcessor比常规的BeanPostProcess多几个方法(这里只列了跟这个问题相关的一个),CommonAnnotationBeanPostProcessor和AutowiredAnnotationBeanPostProcessor就是在postProcessPropertyValues的实现方法中对bean的属性进行设置。
而BeanNameAutoProxyCreator是在postProcessAfterInitialization的实现方法中对bean进行动态代理。AbstractAutoProxyCreator是BeanNameAutoProxyCreator的父类,wrapIfNecessary方法就实现了对bean的动态代理。
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
......
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
......
}
postProcessPropertyValues的执行时机要早于postProcessAfterInitialization,postProcessPropertyValues是在populateBean方法中执行,postProcessAfterInitialization在initializeBean中被调用到。
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
implements AutowireCapableBeanFactory {
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
throws BeanCreationException {
... ...
// Initialize the bean instance.
Object exposedObject = bean;
try {
populateBean(beanName, mbd, instanceWrapper);
if (exposedObject != null) {
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
catch (Throwable ex) {
... ...
}
... ...
}
}
就是说Spring容器在创建bean的过程先设置bean的依赖属性,然后对bean执行动态代理生成代理类对象并将代理类对象放到容器中。在本例中,我们来串一串DemoController这个对象bean的生成过程。
- 首先创建一个DemoContrller对象demoController(默认构造方法),此时DemoController的属性demoPrinter为空;
- 对demoController执行populateBean方法,在populateBean方法中执行postProcessPropertyValues,设置demoPrinter属性,此时demoContrller对象中demoPrinter不为空(在创建DemoPrinter对象的时候也是走了一遍bean的创建初始化过程);
- 对demoController执行initializeBean方法,在initializeBean方法中会调用AbstractAutoProxyCreator(BeanNameAutoProxyCreatord的父类)的postProcessAfterInitialization,这个方法对demoController生成代理类(DemoController$$EnhancerBySpringCGLIB$$5f7bd00.class)返回代理类对象demoControllerProxy(请注意代理对象demoControllerProxy是没有执行populateBean方法的,也就是说代理类对象里面的demoPrinter是空的);
- Spring容器拿到代理类对象demoControllerProxy后,将代理类对象demoControllerProxy放入容器,供以后单例获取bean使用
那么mvc在获取requestmapping的handler对象实际是代理类对象demoControllerProxy。因为DemoController是类不是接口,所以动态代理采用的是cglib组件,我们知道cglib组件是不会对private方法生成代理方法的。也就是说代理类对DemoController#hello生成了代理方法,调用代理类对象的hello最终调用到被代理对象(demoController)的hello方法;而DemoController#helloPri则没有生成代理方法,调用代理类对象的helloPri就是调用代理类的helloPri。第3步明确了代理类对象没有执行populateBean方法,所以代理类对象的demoPrinter是null,所以就抛了NPE异常。
至此,NPE异常原因分析完了。
如何避免
- 代码规范:private、public不要瞎用
- 改掉ctrl-c ctrl-v的习惯,尤其大段大段的复制粘贴
后记:本例给的aop是用BeanNameAutoProxyCreator方式配置的,其实用<aop:config>方式配置完整的aop也是一样会有问题,无论是BeanNameAutoProxyCreator还是<aop:config>都是通过BeanPostProcessor实现的。
最后贴一下cglib组件对DemoController生成的代理类文件(DemoController子类且没有对helloPri生成代理方法):
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.focuse.aopdemo.controller;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.SpringProxy;
import org.springframework.aop.TargetClassAware;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import

本文分析了在Spring MVC环境中,由于Controller中的方法错误地使用了private修饰符,并结合AOP配置,导致运行时出现NullPointerException的原因。深入探讨了Spring容器初始化bean及AOP动态代理的机制,解释了CGLIB不会为private方法生成代理,从而引发NPE的问题根源。
最低0.47元/天 解锁文章
515

被折叠的 条评论
为什么被折叠?



