如图所示,在业务层的实现类BookService中有两个方法:runBefore() 和 runAfter() :
然后再aop包中配置AOP的增强程序:
package com.Alvis.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@Aspect
public class MyAdvice {
// 描述切面方法
@Pointcut("execution(* com.Alvis.service.BookService.*(..))")
public void servicePt() {}
// @Before("MyAdvice.servicePt()")
public void before(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
System.out.println("before ...It has been executed");
System.out.println(Arrays.toString(args));
}
// @After("MyAdvice.servicePt()")
public void after(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
System.out.println("After ...It has been executed");
System.out.println(Arrays.toString(args));
}
@Around("MyAdvice.servicePt()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = pjp.getArgs();
System.out.println("Around ...It has been executed");
// 可以在此修改源方法传入的参数值
args[0] = 1000;
System.out.println(Arrays.toString(args));
Object ret = pjp.proceed(args);
return ret;
}
}

在test文件中编写BookServiceTest类测试方法:
package com.Alvis.service;
import com.Alvis.config.SpringConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// 设定类运行器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class ServiceTest {
private BookService bookService;
@Test
public void testBookServiceBefore() {
ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService bookService = apx.getBean(BookService.class);
bookService.runBefore(90);
}
@Test
public void testBookServiceAfter() {
ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService bookService = apx.getBean(BookService.class);
bookService.runAfter(100);
}
@Test
public void testBookServiceAround() {
ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService bookService = apx.getBean(BookService.class);
bookService.runAround(0 , "张三");
}
}
================================================
before ...It has been executed
[90]
this is runBefore...
age is ...90
进程已结束,退出代码0
================================================
this is runAfter
age is ...100
After ...It has been executed
[100]
进程已结束,退出代码0
@Around可以对源方法传入的参数进行调整
在aop包中的MyAdvice类中的@Around方法描述如图所示:
最后在test文件的BookServiceTest类中对AOP@Around通知修改源方法的参数值作测试
源方法中传入的参数值如图所示:
@Test
public void testBookServiceAround() {
ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
BookService bookService = apx.getBean(BookService.class);
bookService.runAround(0 , "张三");
}
=======================================
Around ...It has been executed
[1000, 张三]
this is runAround
name = 张三 age is ...1000
进程已结束,退出代码0
