1.依赖包
2.为了便于对比,我将我的文件结构也截图出来
3.接口文件ISomeService
package com.buckwheats.test;
public interface ISomeService {
public void doFirst(String user);
public String doSecond(String user);
public void doThird();
}
4.ISomeService的实现类SomeService
package com.buckwheats.test;
public class SomeService implements ISomeService {
@Override
public void doFirst(String user) {
System.out.println("执行doFirst");
}
@Override
public String doSecond(String user) {
System.out.println("执行doSecond");
return "buckwheat";
}
@Override
public void doThird() {
//这里是做测试异常通知,如果不想异常通知可改为System.out.println("执行doThird");
System.out.println("执行doThird" + 3/0);
}
}
5.spring的配置文件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: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="someService" class="com.buckwheats.test.SomeService"/>
<!--注册切面-->
<bean id="myAdvice" class="com.buckwheats.test.MySystem"/>
<!--注册代理-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
6.切面类MySystem
package com.buckwheats.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import java.util.Arrays;
@Aspect
public class MySystem {
//前置方法,指定doFirst为切入点
@Before("execution(* *..ISomeService.doFirst(..))")
public void befor(){
System.out.println("执行前置方法");
}
//后置方法,有参数,指定doFirst为切入点
@After("execution(* *..ISomeService.doFirst(..))")
public void myAfter(JoinPoint jp){
System.out.println("执行后置方法 ,带参数,切入点(方法):"+jp.getSignature().getName());
}
//后置方法,无参数,指定doFirst为切入点
@After("execution(* *..ISomeService.doFirst(..))")
public void AfterReturning(){
System.out.println("执行后置方法 不带参数");
}
//后置方法,无参数,指定doFirst为切入点
@After("execution(* *..ISomeService.doFirst(..))")
public void AfterReturning(JoinPoint jp){
//输出doFirst(String user)传过来的参数
//doFirst(String user)直接输出jp.getArgs()会得到一个数组对象,但是并不知道里面的内容,需要调用Arrays的toString()方法
System.out.println("执行后置方法 带参数 args:"+ Arrays.toString(jp.getArgs()));
}
//环绕通知,可以改变目标方法的返回参数
@Around(value = "execution(* *..ISomeService.doSecond(..))" )
public String myAround(ProceedingJoinPoint result) throws Throwable {
System.out.println("执行环绕通知,前置方法");
Object obj = result.proceed();
//获取doSecond(String user)传过来的参数
//获取doSecond(String user)直接输出jp.getArgs()会得到一个数组对象,但是并不知道里面的内容,需要调用Arrays的toString()方法
System.out.println("执行环绕通知 获取参数 args:"+ Arrays.toString(result.getArgs()));
System.out.println("执行环绕通知,后置方法");
return ((String)obj).toUpperCase();
}
//异常通知,当doThird出现异常时执行
@AfterThrowing(value = "execution(* *..ISomeService.doThird())" , throwing = "ex")
public void myAfterThrowing(Exception ex){
System.out.println("出现异常 e:"+ex);
}
//"最后通知,不管是否出现异常,这个方法都会执行,且在最后执行
@AfterReturning("execution(* *..ISomeService.doThird())")
public void myAfter(){
System.out.println("最后通知,不管是否出现异常,这个方法都会执行");
}
}
7.测试类Test
package com.buckwheats.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test //使用的单元测试包
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("com/buckwheats/test/applicationContext.xml");
ISomeService service = (SomeService) ac.getBean("someService");
service.doFirst("oooo");
System.out.println("=========");
System.out.println(service.doSecond("iii"));
System.out.println("==============");
service.doThird();
}
}
8.输出结果