本来一切正常的项目 , 最近突然出现一个奇怪的问题 . 在某个Controller的方法中 , 任何的bean都没有值 , 全部为null . 
惊奇的是 , 同样的bean在其他Controller中 , 甚至在当前Controller的其他方法中 , 都一切如常 .

后来排查原因 , 原来该方法是private修饰的 , 那之前为什么一切正常呢 , 是最近使用了AOP切面的缘故 . 
用AOP切面后 , Controller中受保护的方法(private或不添加修饰符)注入bean会失败无法注入!

搜集资料所得 , 是因为Spring AOP使用JDK动态代理或者CGLIB来为目标对象创建代理 , 在代理过程中会使用org.springframework.aop.support.AopUtils中一个方法

public static boolean canApply(Pointcut pc, Class targetClass, boolean hasIntroductions) {  if (!pc.getClassFilter().matches(targetClass)) {  return false;  }  MethodMatcher methodMatcher = pc.getMethodMatcher();  IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;  if (methodMatcher instanceof IntroductionAwareMethodMatcher) {  introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;  }  Set classes = new HashSet(ClassUtils.getAllInterfacesForClassAsSet(targetClass));  classes.add(targetClass);  for (Iterator it = classes.iterator(); it.hasNext();) {  Class clazz = (Class) it.next();  Method[] methods = clazz.getMethods();  for (int j = 0; j < methods.length; j++) {  if ((introductionAwareMethodMatcher != null &&  introductionAwareMethodMatcher.matches(methods[j], targetClass, hasIntroductions)) ||  methodMatcher.matches(methods[j], targetClass)) {  return true;  }  }  }     return false;
}  

注意此行代码 : Method[] methods = clazz.getMethods();
是用JAVA反射的方式获取clazz的所有方法 , 但是只能拿到public/protected方法 . 所以private方法无法被代理 , 导致bean注入失败

Controller @Autowired注解 无法注入值为null的问题相关推荐

  1. @autowired注解注入为null_Spring @Autowired 注解自动注入流程是怎么样?

    面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...

  2. Spring @Value取值为null或@Autowired注入失败

    @Value 用于注入.properties文件中定义的内容 @Autowired 用于装配bean 用法都很简单,很直接,但是稍不注意就会出错.下面就来说说我遇到的问题. 前两天在项目中遇到了一个问 ...

  3. @Autowired注解详解——超详细易懂

    @Autowired详解 要搞明白@Autowired注解就是要了解它是什么?有什么作用?怎么用?为什么? 首先了解一下IOC操作Bean管理,bean管理是指(1)spring创建对象 (2)spr ...

  4. Spring的@Autowired注解原理分析

    一. @Autowired的作用 @Autowired常用来作属性的注入,可以作用在构造方法.普通方法.字段.注解.参数上. 将构造函数.字段.设置方法或配置方法标记为由Spring 的依赖注入工具自 ...

  5. 使用@atuowired注解无法注入bean的解决方法(出现Field userMapper in com.peng.service.Impl.UserServiceImpl required a b

    使用@atuowired注解无法注入bean的解决方法(出现Field userMapper in com.peng.service.Impl.UserServiceImpl required a b ...

  6. @Value()读取配置文件属性,读出值为null的问题

    一.问题描述 自定义一个Filter如下: @Component public class JwtFilter extends GenericFilterBean{@Value("${jwt ...

  7. @Autowired注解能用在static属性吗?autowired注入static属性上为null

    @Autowired注解能用在static属性吗? 答案是否定的,我们来测试下: 日志信息已经很明确了,表示static不能被@Autowired进行复制.为什么呢?我们现在就来一起了解其原因. 首先 ...

  8. 解决非controller使用@Autowired注解注入报错为java.lang.NullPointerException问题

    解决非controller使用@Autowired注解注入报错为java.lang.NullPointerException问题 参考文章: (1)解决非controller使用@Autowired注 ...

  9. @autowired注解注入为null_Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案

    相信使用Mybaits的小伙伴们一定会经常编写类似如下的代码: 可以看到 userMapper 下有个红色警告.虽然代码本身并没有问题,能正常运行,但有个警告总归有点恶心.本文分析原因,并列出解决该警 ...

最新文章

  1. 数据结构(05)— 线性单链表实战
  2. java 集合 介绍_java集合类基本简介
  3. 邮件协议(SMTP)性能测试总结(Foxmail邮箱)
  4. 语义匹配(一)【NLP论文复现】Sentence-BERT 句子语义匹配模型的tensorflow实现以及训练Trick
  5. solr创建索引_Solr:创建拼写检查器
  6. AIX学习之--文件系统修复(/home)
  7. c++头文件被c语言调用需要注意什么_嵌入式C语言之模块化编程
  8. 软件测试_Loadrunner_APP测试_性能测试_脚本优化_脚本回放
  9. 函数式编程(一) 认识“编程范式”和“函数”
  10. 手把手教你学项目管理软件project
  11. myeclipse下载_资源共享:常用的编程软件下载链接分享
  12. QQ互联官网使用跳坑
  13. Android 分享到新浪微博
  14. IT十大名言 |IT历史上被引述最多的10句名人名言
  15. python反距离权重法_先从IDW(反距离权重)插值开始吧
  16. ESP8266-NodeMCU网络服务器——通过网页将文件上传到闪存文件系统
  17. 代码改变我的命,我要用代码改变100万女性的命
  18. VxWorks操作系统基础(适合初学者阅读)
  19. 机器人操作系统ROS理论与实践
  20. 如何提高客户转化率---如何分析自己的目标客户.

热门文章

  1. C++ primer 11章关联容器
  2. 利用Linux命令行进行文本按行去重并按重复次数排序
  3. windows安装dcm4chee 出错 check file system group LOSSY_STORAGE for deletion
  4. Java技巧:提高J2SE性能的代码技巧
  5. 回调机制在 Android 监听用户界面操作中的体现
  6. Client端异步Callback的应用与介绍
  7. ArcGIS API for Silverlight 学习笔记(1)
  8. JS-内置对象内置构造函数事件-拖拽轮播图无缝滚动
  9. C++字符串的不同存放类型 (string/char[])
  10. 7-32 中位数 (10 分)