- 新建类不用实现,类中方法名任意
package com.ouc.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAdvice {
//前置通知(两个参数)
public void mybefore(String name,int age){
System.out.println("前置"+":"+name+":"+age);
}
//前置通知(一个参数)
public void mybefore1(String name){
System.out.println("前置1"+":"+name);
}
//后置通知(测试<aop:after>)
public void myafter(){
System.out.println("后置");
}
//后置通知(测试<aop:after-returning>)
public void myafter1(){
System.out.println("后置1");
}
//异常通知
public void mythrow(Exception e){
System.out.println("异常"+e.getMessage());
}
//环绕通知
public Object myarround(ProceedingJoinPoint p) throws Throwable{
System.out.println("执行环绕");
System.out.println("环绕-前置");
Object result = p.proceed();
System.out.println("环绕-后置");
return result;
}
}
- Demo测试类
package com.ouc.demo;
public class Demo {
public void demo1(String name,int age){
System.out.println("demo1");
}
public void demo1(String name){
System.out.println("demo1:一个参数");
}
public void demo1(){
System.out.println("demo1:没有参数");
}
public void demo2() throws Exception{
int i =5/0;
System.out.println("demo2");
}
}
- 配置applicationContext.xml
<aop:aspect>
的ref 属性表示:方法在哪个类中.
<aop: xxxx/>
表示什么通知
method:
当触发这个通知时,调用哪个方法
(1)配置切点:
① execution()括号不能括上args
② 中间的and不能使用&&,由Spring把and解析成&&
③ args(名称) 名称是自定义,其顺序和demo1(参数,参数)一致
(2)前置通知:
① <aop:before/>
arg-names=” 名称” 名称来源于expression=”” 中args(),名称必须一样
② args() 有几个参数,arg-names 里面必须有几个参数
③ arg-names=”” 里面名称必须和通知方法参数名对应
(3)后置通知:
①<aop:after/>
后置通知,是否出现异常都执行
② <aop:after-returing/>
后置通知,只有当切点正确执行时执行
(4)异常通知:
throwing: 异常对象名,必须和通知中方法参数名相同(可以不在通知中声明异常对象)
<aop:after/>
和<aop:after-returing/>
和<aop:after-throwing/>
执行顺序和配置顺序有关
<?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="demo" class="com.ouc.demo.Demo"></bean>
<bean id="myAdvice" class="com.ouc.advice.MyAdvice"></bean>
<aop:config>
<aop:aspect ref="myAdvice">
<aop:pointcut expression="execution(* com.ouc.demo.Demo.demo1(String,int)) and args(name,age)" id="mypoint"/>
<aop:pointcut expression="execution(* com.ouc.demo.Demo.demo1(String)) and args(name)" id="mypoint1"/>
<aop:pointcut expression="execution(* com.ouc.demo.Demo.demo1())" id="mypoint2"/>
<aop:pointcut expression="execution(* com.ouc.demo.Demo.demo2())" id="mypoint3"/>
<aop:before method="mybefore" pointcut-ref="mypoint" arg-names="name,age"/>
<aop:before method="mybefore1" pointcut-ref="mypoint1" arg-names="name"/>
<aop:after method="myafter" pointcut-ref="mypoint2"/>
<aop:after-returning method="myafter1" pointcut-ref="mypoint2"/>
<aop:after-throwing method="mythrow" pointcut-ref="mypoint3" throwing="e"/>
<aop:after method="myafter" pointcut-ref="mypoint3"/>
<aop:after-returning method="myafter1" pointcut-ref="mypoint3"/>
<aop:around method="myarround" pointcut-ref="mypoint2"/>
</aop:aspect>
</aop:config>
</beans>
5. Test测试类
package com.ouc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ouc.demo.Demo;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Demo demo = ac.getBean("demo", Demo.class);
demo.demo1("张三",12);
demo.demo1("李四");
demo.demo1();
demo.demo2();
}
}
- 测试结果