execution(* com.igeekhome.c30.aop.*.*(..))

第一个*表示任意返回值 ,第二个*表示任意类, 第三个*表示任意方法,(...) 表示任意参数;

applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
"><!-- 配置DAO --><bean id="userDAO" class="com.igeek.crm.dao.impl.UserDAOImpl" /><!-- 配置service --><bean id="userService" class="com.igeek.crm.service.impl.UserServiceImpl"><!-- 注入userDAO --><property name="userDAO" ref="userDAO" /></bean><!-- 配置切面 --><bean id="myAspect" class="com.igeek.crm.aspect.MyAspect"/><!-- 配置AOP --><aop:config><!-- 配置一个切入点 --><!-- expression 切入点的表达式,表达式有很多种bean(*Service) 表示有的bean的ID 是Service结尾的Bean都切id : 给切入点表达式起的名字--><aop:pointcut expression="bean(*Service)" id="serviceMethod"/><!-- 切入切面的配置ref:配置要引用的切面bean--><aop:aspect ref="myAspect"><!-- 配置前置切面method:切面方法为引用的切面类中的方法名pointcut:配置引用的切入点--><aop:before method="firstBefor" pointcut-ref="serviceMethod"/><!-- 配置后置通知 --><aop:after-returning returning="returnVal" method="afterReturing" pointcut-ref="serviceMethod"/><!-- 配置环绕通知 --><aop:around method="around" pointcut-ref="serviceMethod"/></aop:aspect></aop:config></beans>

自定义切面 MyAspect类实现功能增强:

package com.igeek.crm.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;/*** * TODO** 2018年10月10日下午7:03:11*/
public class MyAspect {/*** 环绕通知* @param proceedingJoinPoint* @return* @throws Throwable*/public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{Object result = null;System.out.println("---环绕通知开始---");result = proceedingJoinPoint.proceed();//执行被代理对象的方法System.out.println("---环绕通知结束---");return result;}/*** 后置通知* @param joinPoint * @param returnVal 拦截方法的返回值*/public void afterReturing(JoinPoint joinPoint,Object returnVal){System.out.println("---------后置通知-----得到返回值:"+returnVal+"-----");}//第一个前置通知public void firstBefor(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();System.out.println("拦截的方法名称:"+methodName);String className = joinPoint.getTarget().getClass().getName();System.out.println("拦截对象的类名:"+className);System.out.println("代理对象:"+joinPoint.getThis().getClass().getName());System.out.println("第一个前置通知......");}
}

================================================================================

例2:

package com.igeekhome.c30.spring_aop_xml;import java.util.Arrays;import org.aspectj.lang.JoinPoint;public class CalculatorAspect {public void beforeMethod(JoinPoint joinPoint){System.out.println("CalculatorAspect1--------->"+joinPoint.getSignature().getName()+" method begin with "+Arrays.toString( joinPoint.getArgs()));}public void afterMethod(JoinPoint joinPoint){System.out.println("CalculatorAspect1--------->"+joinPoint.getSignature().getName()+" method end");}
}
package com.igeekhome.c30.spring_aop_xml;import java.util.Arrays;import org.aspectj.lang.JoinPoint;public class CalculatorAspect2 {public void beforeMethod(JoinPoint joinPoint){System.out.println("CalculatorAspect2--------->"+joinPoint.getSignature().getName()+" method begin with "+Arrays.toString( joinPoint.getArgs()));}public void afterMethod(JoinPoint joinPoint){System.out.println("CalculatorAspect2--------->"+joinPoint.getSignature().getName()+" method end");}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><bean id="calculator" class="com.igeekhome.c30.spring_aop_xml.CalculatorImpl"></bean><!-- 使用AOP肯定需要定义切面 --><bean id="aspect" class="com.igeekhome.c30.spring_aop_xml.CalculatorAspect"></bean><bean id="aspect2" class="com.igeekhome.c30.spring_aop_xml.CalculatorAspect2"></bean><!-- 配置AOP --><aop:config><aop:pointcut expression="execution(* com.igeekhome.c30.spring_aop_xml.*.*(..))" id="pointcut"/><aop:aspect ref="aspect" order="2"><aop:before method="beforeMethod" pointcut-ref="pointcut"/><aop:after method="afterMethod" pointcut-ref="pointcut"/></aop:aspect><aop:aspect ref="aspect2" order="1"><aop:before method="beforeMethod" pointcut-ref="pointcut"/><aop:after method="afterMethod" pointcut-ref="pointcut"/></aop:aspect></aop:config>
</beans>

expression配置:

任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了MyInterface接口的所有类,如果MyInterface不是接口,限定MyInterface单个类.
this(com.test.spring.aop.pointcutexp.MyInterface)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.带有@MyTypeAnnotation标注的所有类的任意方法.
@within(com.elong.annotation.MyTypeAnnotation)
@target(com.elong.annotation.MyTypeAnnotation)
带有@MyTypeAnnotation标注的任意方法.
@annotation(com.elong.annotation.MyTypeAnnotation)
***> @within和@target针对类的注解,@annotation是针对方法的注解参数带有@MyMethodAnnotation标注的方法.
@args(com.elong.annotation.MyMethodAnnotation)
参数为String类型(运行是决定)的方法.
args(String)

Spring配置实现AOP相关推荐

  1. JAVA基础加强(张孝祥)_类加载器、分析代理类的作用与原理及AOP概念、分析JVM动态生成的类、实现类似Spring的可配置的AOP框架...

    1.类加载器 ·简要介绍什么是类加载器,和类加载器的作用 ·Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader ...

  2. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. 1 package com.y ...

  3. Spring中的AOP(三)——基于Annotation的配置方式(一)

    为什么80%的码农都做不了架构师?>>>    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是 ...

  4. java学习day40(Spring)spring中的aop和基于XML以及注解的AOP配置

    第1章 AOP 的相关概念[理解] 1.1AOP 概述 1.1.1 什么是 AOP AOP :全称是 Aspect Oriented Programming 即:面向切面编程. 简单的说它就是把我们程 ...

  5. 一文读懂Spring中的AOP机制

    一.前言 这一篇我们来说一下 Spring 中的 AOP 机制,为啥说完注解的原理然后又要说 AOP 机制呢? 1.标记日志打印的自定义注解 @Target({ElementType.METHOD}) ...

  6. Spring 框架的AOP之注解的方式

    1. 环境搭建 1.1 导入 jar 包 Spring 框架的基本开发包(6个); Spring 的传统AOP的开发包 spring-aop-4.3.10.RELEASE org.aopallianc ...

  7. Spring系列之AOP实现的两种方式

    Spring只支持XML方式而没有实现注解的方式(也叫AspectJ方式)的AOP,所以要使用@Aspect注解,只能引入AspectJ相关的 jar 包: aopalliance-1.0.jar 和 ...

  8. Spring源码-AOP(六)-自动代理与DefaultAdvisorAutoProxyCreator

    2019独角兽企业重金招聘Python工程师标准>>> Spring AOP 源码解析系列,建议大家按顺序阅读,欢迎讨论 Spring源码-AOP(一)-代理模式 Spring源码- ...

  9. 自己动手实现的 Spring IOC 和 AOP - 下篇

    1. 背景 本文承接上文,来继续说说 IOC 和 AOP 的仿写.在上文中,我实现了一个很简单的 IOC 和 AOP 容器.上文实现的 IOC 和 AOP 功能很单一,且 IOC 和 AOP 两个模块 ...

最新文章

  1. 启动 Tensorboard
  2. spring in action小结4.1
  3. python中的np where_python – np.where在我的熊猫中不起作用
  4. Laravel 不同环境加载不同的.env文件
  5. poj2060Taxi Cab Scheme(二分图匹配)
  6. 工程计算软件_软件工程师的计算机基础理论知识体系
  7. 关于诺顿误杀系统文件的解决方法
  8. 验证码图片生成和验证
  9. 安装SVN后,更新项目没有对号等图标
  10. 使用vue-giant-tree根据id定位位置
  11. Cocos野兽派 | 如何创造真正的优秀游戏
  12. H.264中SPS、PPS和IDR
  13. 第5周编程题在线测试
  14. 查看Chrome浏览器版本及下载对应的webdriver,并下载安装
  15. 项目管理之项目干系人
  16. Kernel panic - not syncing VFS Unable to mount root fs on
  17. pprint和print区别
  18. vue延迟渲染组件_性能优化之组件懒加载: Vue Lazy Component 介绍
  19. 国密sm2 js加密后台解密,sm3 js、后台加密,sm4 后台加密
  20. ubuntu命令行 播放音乐

热门文章

  1. Java4Android视频笔记
  2. Laravel-事件简单使用
  3. Jfinal文件上传基础路径问题,windows下会以项目根路径为基础路径
  4. 2018.8.2 python中is和==的区别
  5. Unity3D碰撞触发函数
  6. vue2.0+echarts可视化图形开发中遇到的问题总结
  7. java-银行业务调度系统《十一》
  8. 让系统在内存中高速运行
  9. 打不开文件F:\vmware -副本\Ubuntu 16.vmdk:该虚拟机的磁盘已经由虚拟机或者快照使用
  10. Chrome浏览器中的【开发者工具】---只对当前窗口有效---每个窗口都可以打开一个【开发者工具】