4.0.0com.exampledemo0.0.1-SNAPSHOTdemoDemo project for Spring Boot1.8UTF-8UTF-82.3.7.RELEASEorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-aoporg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.springframework.bootspring-boot-dependencies${spring-boot.version}pomimportorg.apache.maven.pluginsmaven-compiler-plugin3.8.11.81.8UTF-8org.springframework.bootspring-boot-maven-plugin2.3.7.RELEASEcom.example.demo.DemoApplicationrepackagerepackage
测试代码
package cn.tedu;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
public class MyAopTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
UserService us = context.getBean(UserService.class);
us.addUser();
}
}
@Configuration
@ComponentScan("cn.tedu")
@EnableAspectJAutoProxy
class MyConfig{
}
interface UserService{
void addUser();
}
@Service
class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("UserServiceImpl.addUser();");
}
}
@Component
@Aspect
@Order(1)
class MyAop1 {
@Pointcut("within(cn.tedu.UserServiceImpl)")
public void pc() {
}
@Before("pc()")
public void Before() {
System.out.println("aop1.before() 前置通知");
}
@AfterReturning("pc()")
public void afterReturning() {
System.out.println("aop1.afterRetruning() 返回后通知");
}
@After("pc()")
public void after() {
System.out.println("aop1.after() 后置通知");
}
@AfterThrowing("pc()")
public void afterThrowing() {
System.out.println("aop1.afterThrowing() 抛异常后通知");
}
@Around("pc()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("aop1.around_before() 环绕前");
Object result = joinPoint.proceed();
System.out.println("aop1.around_after() 环绕后");
return result;
}
}
@Component
@Aspect
@Order(2)
class MyAop2 {
@Pointcut("within(cn.tedu.UserServiceImpl)")
public void pc() {
}
@Before("pc()")
public void Before() {
System.out.println("aop2.before() 前置通知");
}
@AfterReturning("pc()")
public void afterReturning() {
System.out.println("aop2.afterRetruning() 返回后通知");
}
@After("pc()")
public void after() {
System.out.println("aop2.after() 后置通知");
}
@AfterThrowing("pc()")
public void afterThrowing() {
System.out.println("aop2.afterThrowing() 抛异常后通知");
}
@Around("pc()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("aop2.around_before() 环绕前");
Object result = joinPoint.proceed();
System.out.println("aop2.around_after() 环绕后");
return result;
}
}