spring学习笔记(20)——基于配置文件方式配置AOP
来源:互联网 发布:使命召唤二战 知乎 编辑:程序博客网 时间:2024/04/30 02:59
基于注解的方式配置AOP
查看先前的笔记
- spring学习笔记(15)——AOP基础
- spring学习笔记(16)——AOP之前后置通知
- spring学习笔记(17)——返回通知&异常通知&环绕通知
- spring学习笔记(18)——切面的优先级
- spring学习笔记(19)——重用切点表达式
基于配置文件方式配置AOP
功能代码
package com.zj.asceptj.xml;public class CalcultorImpl implements Calcultor{ @Override public int add(int a, int b) { int result = a + b; return result; } @Override public int sub(int a, int b) { int result = a - b; return result; }}
切面
package com.zj.asceptj.xml;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;public class LoggingAscept { public void beforeMethod(JoinPoint joinpoint){ //注意,JoinPoint来自org.aspectj.lang.JoinPoint,小心导错包 //方法名 String methodName = joinpoint.getSignature().getName(); //方法参数 List<Object> args = Arrays.asList(joinpoint.getArgs()); System.out.println("method "+methodName+" begin:"+args); } public void afterMethod(JoinPoint joinpoint){ //注意,JoinPoint来自org.aspectj.lang.JoinPoint,小心导错包 //方法名 String methodName = joinpoint.getSignature().getName(); //方法参数 List<Object> args = Arrays.asList(joinpoint.getArgs()); System.out.println("method "+methodName+" end:"+args); } public void afterReturning(JoinPoint joinPoint,Object result){ //方法名 String methodName = joinPoint.getSignature().getName(); System.out.println("method "+methodName+" end:"+result); } public void afterThrowing(JoinPoint joinPoint,Exception ex){ //方法名 String methodName = joinPoint.getSignature().getName(); System.out.println("method "+methodName+" occurs:"+ex); } public Object around(ProceedingJoinPoint pjd){ Object result = null; String methodName = pjd.getSignature().getName(); try { //前置通知 System.out.println("method:"+methodName+" begins with "+Arrays.asList(pjd.getArgs())); //执行目标方法 result = pjd.proceed(); //返回通知 System.out.println("method:"+methodName+" end with "+result); } catch (Throwable e) { // 异常通知 System.out.println("method:"+methodName+" occurs exception "+e); } //后置通知 System.out.println("method ends"); return result; }}
配置文件
<?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" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean --> <bean id="calcultorImpl" class="com.zj.asceptj.xml.CalcultorImpl"></bean> <bean id="loggingAscept" class="com.zj.asceptj.xml.LoggingAscept"></bean> <!-- 配置AOP --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut expression="execution(* com.zj.asceptj.xml.*.*(..))" id="pointcut"/> <!-- 配置切面以及通知 --> <aop:aspect ref="loggingAscept" order="1"> <!-- 前置通知 --> <aop:before method="beforeMethod" pointcut-ref="pointcut"/> <!-- 后置通知 --> <aop:after method="afterMethod" pointcut-ref="pointcut"/> <!-- 返回通知 --> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> <!-- 异常通知 --> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/> <!-- 环绕通知 --> <!-- <aop:around method="around" pointcut-ref="pointcut"/> --> </aop:aspect> <!-- 可以配置多个切面 --> </aop:config></beans>
0 0
- spring学习笔记(20)——基于配置文件方式配置AOP
- spring学习笔记(22)----基于配置文件的方式来配置AOP
- Spring笔记5-基于配置文件的方式配置AOP
- 20.Spring学习笔记_基于配置文件的方式来配置 AOP(by尚硅谷_佟刚)
- Spring学习笔记(四)Spring配置文件方式实现AOP
- Spring4学习笔记-AOP(基于配置文件的方式)
- Spring中的AOP(六)——基于XML配置文件方式的AOP
- Spring中的AOP(七)——基于XML配置文件方式的AOP
- Spring中的AOP(六)——基于XML配置文件方式的AOP
- Spring-AOP:基于配置文件方式的AOP
- Spring中基于配置文件的方式来配置AOP
- spring学习(七)—AOP通过配置文件方式实现
- Spring中的AOP—基于Annotation的配置方式
- Spring 学习笔记(5)—— 简化xml配置文件的配置方式
- 基于配置文件的方式来配置 AOP
- 基于xml方式配置aop的配置文件
- Spring AOP学习笔记(4):AOP切面优先级&切面表达式重用&xml配置方式
- Spring中的AOP(二)——基于Annotation的配置方式(一)
- win7上安装CentOS7双系统(U盘启动)
- 根据两点经纬度坐标计算距离
- Best_Cow_Line
- 微营销好标题之喜闻乐见(微营销标题吸引粉丝篇-6)
- Hadoop : MapReduce中的Shuffle和Sort分析
- spring学习笔记(20)——基于配置文件方式配置AOP
- UI--从学习styleable自定义view属性到一点儿更有意思的尝试
- Security Analytics and Threat Analysis brighttalk
- 应用程序的安装
- 唯一性约束和唯一性索引的区别
- HDOJ1216 Assistance Required 打表
- 微信之自定义菜单
- 4.7 蚂蚁爬杆
- ACM必备(学完一个就加亮一个)不多,就这些!