public class MyAdvice { public void before(){ System.out.println("前:org.springframework.aop.MethodBeforeAdvice"); } public void afterReturning(){ System.out.println("返回后: org.springframework.aop.AfterReturningAdvice"); } public void afterThrowing(){ System.out.println("抛出后: org.springframework.aop.ThrowsAdvice"); } public void around(){ System.out.println("周围: org.aopalliance.intercept.MethodInterceptor"); } public void introduction(){ System.out.println("引入:org.springframework.aop.IntroductionInterceptor"); } }
import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.ThrowsAdvice; public class AopOne implements AfterReturningAdvice, MethodBeforeAdvice ,ThrowsAdvice{ /** * org.springframework.aop.MethodBeforeAdvice 接口 必须实现 下面方法 */ public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { advice.before(); } /** * org.springframework.aop.AfterReturningAdvice 接口 必须实现 下面方法 */ public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { advice.afterReturning(); } //org.springframework.aop.ThrowsAdvice接口不包含任何方法: 它只是一个标记接口用来标识所给对象实现了一个或者多个针对特定类型的异常通知方法。 //这些方法应当满足下面的格式: afterThrowing([Method, args, target], subclassOfThrowable) public void afterThrowing(Throwable throwable) { advice.afterThrowing(); } private MyAdvice advice; public MyAdvice getAdvice() { return advice; } public void setAdvice(MyAdvice advice) { this.advice = advice; } }
import java.lang.reflect.Method; public class AopTwo { /** * org.springframework.aop.MethodBeforeAdvice 接口 必须实现 下面方法 */ public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { advice.before(); } /** * org.springframework.aop.AfterReturningAdvice 接口 必须实现 下面方法 */ public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { advice.afterReturning(); } //org.springframework.aop.ThrowsAdvice接口不包含任何方法: 它只是一个标记接口用来标识所给对象实现了一个或者多个针对特定类型的异常通知方法。 //这些方法应当满足下面的格式: afterThrowing([Method, args, target], subclassOfThrowable) public void afterThrowing(Throwable throwable) { advice.afterThrowing(); } private MyAdvice advice; public MyAdvice getAdvice() { return advice; } public void setAdvice(MyAdvice advice) { this.advice = advice; } }
public class AppOne { /** * 不用AOP 对一个方法进行包装的步骤 */ public void sysMethod() { advice.before();// 事件 执行 前处理 try { // 具体执行事件 用 一条输出 语句替代 System.out.println("【 我是 被修饰 的方法。。】"); } catch (Exception e) { e.printStackTrace(); advice.afterThrowing();// 事件执行抛出异常后处理 } advice.afterReturning();// 事件成功执行完成 后处理 } private MyAdvice advice; public MyAdvice getAdvice() { return advice; } public void setAdvice(MyAdvice advice) { this.advice = advice; } }
public class TargetBean1 { public void tartgetMethod (){ System.out.println("【我是 被包装目标方法】"); } }
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id="myad" class="com.zxiaoyao.aop.MyAdvice" /> <!-- 通知1 --> <bean id="adviceOne" class="com.zxiaoyao.aop.AopOne"> <property name="advice" ref="myad" /> </bean> <bean id="adviceTwo" class="com.zxiaoyao.aop.AopTwo"> <property name="advice" ref="myad" /> </bean> <!-- 切点1 (切点定义 方法1) --> <bean id="pointcutOne" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*tartgetMethod" /> </bean> <!-- 联合切点和通知 1 --> <bean id="pointcutAdvisorOne" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="adviceOne" /> <property name="pointcut" ref="pointcutOne" /> </bean> <!-- 定义AspectJ 切点(方法2) --> <!-- 应用AspectJ样式的表达式 --> <bean id="pointcutTwo" class="org.springframework.aop.aspectj.AspectJExpressionPointcut"> <property name="expression" value="execution(* *.tartgetMethod(..))"></property> </bean> <!-- 联合切点和通知 2 --> <bean id="pointcutAdvisorTwo" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="adviceOne"></property> <property name="pointcut" ref="pointcutTwo"></property> </bean> <!-- 应用AspectJ 样式 可省略 定义切点 --> <bean id="pointcutAdvisorThree" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor"> <property name="advice" ref="adviceOne"></property> <property name="expression" value="execution(* *.tartgetMethod(..))"></property> </bean> <bean id="beantarget1" class="com.zxiaoyao.aop.TargetBean1"> </bean> <!-- 代理Bean1 (方法1) --> <bean id="bean1" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="beantarget1"/> <property name="interceptorNames" value="pointcutAdvisorOne"></property> </bean> <!-- 设置抽象类 ProxyFactoryBean (方法2) --> <bean id="absbean" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames"> <list> <value>pointcutAdvisorOne</value> </list> </property> </bean> <!-- 可配多个 代理bean 继承 抽象bean --> <bean id="bean2" parent="absbean"> <property name="target" ref="beantarget1"></property> </bean> <bean id="bean3" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="beantarget1"/> <property name="interceptorNames" value="pointcutAdvisorTwo"></property> </bean> <bean id="bean4" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="beantarget1"></property> <property name="interceptorNames" value="pointcutAdvisorThree"></property> </bean> <bean id="bean5" class="com.zxiaoyao.aop.TargetBean1"> </bean> </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean id="myad" class="com.zxiaoyao.aop.MyAdvice" /> <!-- 纯粹 aop 配置 --> <!-- 定义顶级aop元素 --> <aop:config> <!-- 定义一个切面 --> <aop:aspect ref="myad"> <!-- 后通知 不考虑方法是否成功返回--> <aop:after method="afterReturning" pointcut="execution(* *.tartgetMethod(..))"/> <!-- 返回后通知 --> <aop:after-returning method="afterReturning" pointcut="execution(* *.tartgetMethod(..))"/> <!-- 前通知 --> <aop:before method="before" pointcut="execution(* *.tartgetMethod(..))"/> <!-- 异常抛出通知 --> <aop:after-throwing method="afterThrowing" pointcut="execution(* *.tartgetMethod(..))"/> <!-- 周围通知 周围通知 定义 --> <aop:around method="afterThrowing" pointcut="execution(* *.tartgetMethod(..))"/> </aop:aspect> <!-- 不想每个个 通知里都 配切点 <!-- <aop:aspect ref="myad"> <aop:pointcut expression="execution(* *.tartgetMethod(..))" id="pointcut1"/> <aop:after method="afterReturning" pointcut-ref="pointcut1"/> <aop:before method="before" pointcut-ref="pointcut1"/> </aop:aspect> --> </aop:config> <bean id="bean1" class="com.zxiaoyao.aop.TargetBean1"> </bean> </beans>
public class Mytest { public static void main(String[] args) throws InterruptedException { test4(); } public static void test4(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext4.xml"); TargetBean1 t1 = (TargetBean1)context.getBean("bean1"); t1.tartgetMethod(); } public static void test3(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml"); TargetBean1 t = (TargetBean1)context.getBean("bean1"); t.tartgetMethod(); TargetBean1 t2= (TargetBean1)context.getBean("bean3"); t2.tartgetMethod(); TargetBean1 t4 = (TargetBean1)context.getBean("bean4"); t4.tartgetMethod(); } }