applicationContext.xml配置AOP切面编程
Computer.java
package com.wh.aop2;
public class Computer {
public void play01(){
System.out.println("一号玩家!");
}
public void play02(){
System.out.println("二号玩家!");
System.out.println(10/0);
}
public void play03(){
System.out.println("三号玩家!");
}
public void play04(){
System.out.println("四号玩家!");
}
public void play05(){
System.out.println("五号玩家!");
}
public void play06(){
System.out.println("六号玩家!");
}
}
AopProxy.java
package com.wh.aop2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class AopProxy {
public void doBefore01() {
System.out.println("无参数前置通知:");
}
public void doBefore02() {
System.out.println("有参数前置通知:");
}
public void doAround(ProceedingJoinPoint p) {
System.out.println("环绕之前置通知:");
Object obj = null;
try {
obj = p.proceed();
System.out.println("环绕之后置通知: " + obj);
}
catch (Throwable e) {
System.out.println("环绕之异常通知: " + e.getMessage());
}
finally {
System.out.println("环绕之最终通知:");
}
}
}
applicationContext.xml
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="computer" class="com.wh.aop.Computer"/> <bean id="testBefore" class="com.wh.aop.TestBefore"/> <aop:config> <aop:pointcut expression="execution(* com.wh.aop.Computer.add(*,*))" id="computerAddCut"/> <aop:pointcut expression="execution(* com.wh.aop.Computer.div(*,*))" id="computerDivCut"/> <!-- AOP前置通知 --> <aop:aspect ref="testBefore"> <aop:before method="doBefore" pointcut-ref="computerAddCut"/> </aop:aspect> <!-- AOP后置通知 --> <aop:aspect ref="testBefore"> <aop:after-returning method="doAfter" pointcut-ref="computerAddCut" returning="ret"/> </aop:aspect> <!-- AOP异常通知 --> <aop:aspect ref="testBefore"> <aop:after-throwing method="doThrow" pointcut-ref="computerDivCut" throwing="e"/> </aop:aspect> </aop:config> <import resource="applicationContext2.xml"/> </beans>
applicationContext2.xml
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="computer2" class="com.wh.aop2.Computer"/> <bean id="aopProxy2" class="com.wh.aop2.AopProxy"/> <aop:config> <aop:pointcut expression="execution(* com.wh.aop2.Computer.*(..))" id="computerCut2"/> <aop:aspect ref="aopProxy2"> <aop:around method="doAround" pointcut-ref="computerCut2"/> </aop:aspect> </aop:config> </beans>
TestAop.java
package com.wh.aop2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
@Test
public void test01(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play01();
}
@Test
public void testAround(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play01();
}
/**
环绕之前置通知:
一号玩家!
环绕之后置通知: null
环绕之最终通知:
*/
@Test
public void testAround2(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play02();
}
/**
环绕之前置通知:
二号玩家!
环绕之异常通知: / by zero
环绕之最终通知:
*/
//小结:在环绕通知中:正常情况下,四个通知都会出现,若出现异常,只有后置通知不会出现!
//通知顺序为:前置、异常、最终、后置
}

浙公网安备 33010602011771号