之前用springAOP做了个操作日志记录,这次在往其他类上使用的时候,service一直注入失败,找了网上好多内容,发现大家都有类似的情况出现,但是又和自己的情况不太符合。后来总结自己的情况发现:方法为private修饰的,在AOP适配的时候会导致service注入失败,并且同一个service在其他的public方法中就没有这种情况,十分诡异。

  结合查阅的资料进行了分析:在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();只能拿到public方法。

  execution(* *(..)) 可以匹配public/protected的,因为public的有匹配的了,目标类就代理了,,,再进行切入点匹配时也是能匹配的,而且cglib方式能拿到包级别/protected方法,而且包级别/protected方法可以直接通过反射调用。

  private 修饰符的切入点 无法匹配 Method[] methods = clazz.getMethods(); 这里的任何一个,因此无法代理的。 所以可能因为private方法无法被代理,导致@Autowired不能被注入。

  修正办法:

1、将方法修饰符改为public;

2、使用AspectJ来进行注入。

SpringAOP导致@Autowired依赖注入失败的更多相关文章

  1. 解决 Springboot中Interceptor拦截器中依赖注入失败

    问题: 在Springboot拦截器Interceptor中使用@Resource依赖注入时,发现运行的时候被注解的对象居然是null,没被注入进去 原配置为: @Configurationpubli ...

  2. @Autowired自动注入失败

    新手注意的问题 package cn.ryq.web.controller; import cn.ryq.domain.company.Company;import cn.ryq.service.co ...

  3. SpringMVC的filter怎么使用Autowired依赖注入bean

      有的时候根据我们业务的需要,我们需要在web项目中定义一个自己的filter,并想在这个filter中使用@Autowired注入bean供我们使用.如果直接使用的话是不行的,需要我们在xml文件 ...

  4. @Autowired注解注入失败,提示could not autowire的解决办法

    autowire异常主要由三个情况发生的 像上面的情况是BrandDao没有注入, 1.你的BrandServiceImpl必须以@Service或@Component注解才行. 2.自动写入的时候把 ...

  5. Springboot 拦截器 依赖注入失败

    解决方案2种. ====1 https://blog.csdn.net/shunhua19881987/article/details/78084679 ====2 https://www.cnblo ...

  6. 当spring 对象@Autowired 注入失败或者创建对象Bean失败、No qualifying bean/Error creating bean 的失败情形分析和解决方案

    错误信息 今天开发的过程中突然出现如下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: N ...

  7. Spring 依赖注入优化

    Spring 依赖注入优化 原创: carl.zhao SpringForAll社区 今天 Spring 最大的好处就是依赖注入,关于什么是依赖注入,在Stack Overflow上面有一个问题,如何 ...

  8. 一文搞懂│php 中的 DI 依赖注入

    目录 什么是 DI / 依赖注入 依赖注入出现的原因 简单的依赖注入 高阶的依赖注入 依赖注入的应用 依赖注入高阶优化 什么是 DI / 依赖注入 依赖注入DI 其实本质上是指对类的依赖通过构造器完成 ...

  9. static类型autowired 注入失败

    原代码:注入commonService对象失败 @Autowired private static CommonService commonService; public static List< ...

随机推荐

  1. Java 之Integer相等比较

    1.问题提出 今天在和同事讨论问题的时候,无意间谈到了Integer对象的比较,先看下代码: package test; public class IntegerEqual { /** * @para ...

  2. 转载:Linux Used内存到底到哪里去了?

    转自:http://blogread.cn/it/article/6264?f=wb2 有时在Linux下会碰到这样的问题:ps aux看到的RSS内存只有不到30M,但是free看到内存却已经使用了 ...

  3. 负载均衡DNS和反向代理优缺点

    负载均衡 (Load Balancing) 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性. 负载均衡(又 ...

  4. leetcode第28题--Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 分析:题目意思很容易理解,就是不用乘除法和模运 ...

  5. 在OpenWrt上编写自己的硬件操作程序

    上一篇文章中有写到如何使用OPENWRT的SDK,这里继续,写怎么在上面开发自己的应用程序. 我欲在OpenWrt上编写一个软件,它能够去读取某个AD芯片的多通道采样值. 在看这篇文章之前请看这官方的 ...

  6. js 拖拽实现

    代码参考 <!DOCTYPE html> <html lang="en"> <title>拖拽</title> <head&g ...

  7. android ListView隐藏FooterView(headerView)

    安卓开发.正在使用listView时间,我们经常使用的footerView要么headerView 给ListView添加footer和header很多时间.它会基于隐藏和显示的情况下. 因为foot ...

  8. Apache2.4.x与Apache2.2.x的一些区别

    改用Apache2.4一段时间了,一直没发现它和Apache2.2的有什么区别,一些基本配置都是差不多,直到前几天配置虚拟主机是才发现了一些小小的不同 一直以来我都是在htdocs目录下配置虚拟主机的 ...

  9. 在SSIS中使用自定义的DLL文件

    原文:在SSIS中使用自定义的DLL文件 步骤1.开发dll(需要签名) using System;using System.Collections.Generic;using System.Text ...

  10. 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10(转载)

    在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html> ...