連接:https://pan.baidu.com/s/1qS-AaZHSLUwxYSjc9PHDYA
提取碼:np2q
java
6.1.2.1 applicationContext.xml 中 spring
<?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/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"> <!-- 配置通知類對象,在切面中引入 --> <bean id="mybefore" class="com.suncl.test.MyBeforeAdvice"></bean> <bean id="myafter" class="com.suncl.test.MyAfterAdvice"></bean> <bean id="myarround" class="com.suncl.test.MyArround"></bean> <bean id="mythrow" class="com.suncl.test.MyThrow"></bean> <!-- 配置切面 --> <aop:config> <!-- 配置切點 --> <aop:pointcut expression="execution(* com.suncl.test.Demo.demo())" id="mypoint"/> <aop:pointcut expression="execution(* com.suncl.test.Demo.demo1())" id="mypoint1"/> <!-- 前置通知 --> <aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/> <!-- 後置通知 --> <aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/> <!-- 環繞通知 --> <aop:advisor advice-ref="myarround" pointcut-ref="mypoint" /> <!--異常通知--> <aop:advisor advice-ref="mythrow" pointcut-ref="mypoint1" /> </aop:config> <!-- 配置 Demo 類,測試使用 --> <bean id="demo" class="com.suncl.test.Demo"></bean> <!--<!– aspect方式配置異常通知 –>--> <!--<bean id="myThrowAdvice" class="com.suncl.test.MyThrowAdvice"></bean>--> </beans>
6.1.2.2 實體類express
package com.suncl.test; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /** * Created by SCL-PC on 2019/2/28. */ public class MyBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("執行前置通知"); } }
package com.suncl.test; import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; /** * Created by SCL-PC on 2019/2/28. */ public class MyAfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("執行後置通知"); } }
package com.suncl.test; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * Created by SCL-PC on 2019/2/28. */ public class MyArround implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("環繞-前置"); Object result = methodInvocation.proceed();//放行,調用切點方式 System.out.println("環繞-後置"); return result; } }
package com.suncl.test; import org.springframework.aop.ThrowsAdvice; /** * Created by SCL-PC on 2019/2/28. */ public class MyThrow implements ThrowsAdvice { public void afterThrowing(Exception ex) throws Throwable { System.out.println("MyThrow執行異常經過-schema-base 方式 "); } }
package com.suncl.test; /** * Created by SCL-PC on 2019/2/28. */ public class Demo { public void demo(){ System.out.println("執行了demo方法"); } public void demo1(){ System.out.println("執行了demo1方法"); int i = 1/0; } public void demo2(){ System.out.println("執行了demo2方法"); int i = 1/0; } }
package com.suncl.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by SCL-PC on 2019/2/28. */ public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Demo demo = ac.getBean("demo",Demo.class); demo.demo(); demo.demo1(); } }
信息: Loading XML bean definitions from class path resource [applicationContext.xml] 執行前置通知 環繞-前置 執行了demo方法 環繞-後置 執行後置通知 執行了demo1方法 MyThrow執行異常經過-schema-base 方式 Exception in thread "main" java.lang.ArithmeticException: / by zero at com.suncl.test.Demo.demo1(Demo.java:14)