Spring provides simple and powerful ways of writing custom aspects by using either a schema-based approach or the @AspectJ annotation style. Both of these styles offer fully typed advice and use of the AspectJ pointcut language while still using Spring AOP for weaving.
Spring 通过使用基于模式的方法或 @AspectJ 注解样式,提供了编写自定义切面的简单而强大的方法。这两种样式都提供了完全类型化的通知,并使用了 AspectJ 切入点表达式语言,同时仍使用 Spring AOP 进行通知的织入。
public class FinanceService {
public void addMoney(double money) {
System.out.println("FinanceService 收钱 === " + money);
}
public double subtractMoney(double money) {
System.out.println("FinanceService 付钱 === " + money);
return money;
}
public double getMoneyById(String id) {
System.out.println("FinanceService 查询账户,id为" + id);
return Math.random();
}
}
public class Logger {
public void beforePrint() {
System.out.println("Logger beforePrint run ......");
}
public void afterPrint() {
System.out.println("Logger afterPrint run ......");
}
public void afterReturningPrint() {
System.out.println("Logger afterReturningPrint run ......");
}
public void afterThrowingPrint() {
System.out.println("Logger afterThrowingPrint run ......");
}
}
@Component
public class FinanceService { ... }
@Component
public class OrderServiceImpl implements OrderService { ... }
@Aspect
@Component
public class Logger { ... }
@Aspect
@Component
public class Logger {
@Before("execution(public * com.linkedbear.spring.aop.a_xmlaspect.service.FinanceService.*(..))")
public void beforePrint() {
System.out.println("Logger beforePrint run ......");
}
}
@Aspect
@Component
public class Logger {
@Before("execution(public * com.linkedbear.spring.aop.b_aspectj.service.FinanceService.*(..))")
public void beforePrint() {
System.out.println("Logger beforePrint run ......");
}
@After("execution(* com.linkedbear.spring.aop.b_aspectj.service.*.*(String)))")
public void afterPrint() {
System.out.println("Logger afterPrint run ......");
}
@AfterReturning("execution(* com.linkedbear.spring.aop.b_aspectj.service.*.*(String)))")
public void afterReturningPrint() {
System.out.println("Logger afterReturningPrint run ......");
}
@AfterThrowing("execution(* com.linkedbear.spring.aop.b_aspectj.service.*.*(String)))")
public void afterThrowingPrint() {
System.out.println("Logger afterThrowingPrint run ......");
}
}
@Configuration
@ComponentScan("com.linkedbear.spring.aop.b_aspectj")
@EnableAspectJAutoProxy
public class AspectJAOPConfiguration {
}
public class AnnotationAspectJApplication {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AspectJAOPConfiguration.class);
FinanceService financeService = ctx.getBean(FinanceService.class);
financeService.addMoney(123.45);
financeService.subtractMoney(543.21);
financeService.getMoneyById("abc");
}
}
Logger beforePrint run ......
FinanceService 收钱 === 123.45
Logger beforePrint run ......
FinanceService 付钱 === 543.21
Logger beforePrint run ......
FinanceService 查询账户,id为abc
Logger afterReturningPrint run ......
Logger afterPrint run ......
@Around("execution(public * com.linkedbear.spring.aop.b_aspectj.service.FinanceService.addMoney(..))")
public void aroundPrint() {
}
@Around("execution(public * com.linkedbear.spring.aop.b_aspectj.service.FinanceService.addMoney(..))")
public Object aroundPrint(ProceedingJoinPoint joinPoint) {
}
@Around("execution(public * com.linkedbear.spring.aop.b_aspectj.service.FinanceService.addMoney(..))")
public Object aroundPrint(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Logger aroundPrint before run ......");
try {
Object retVal = joinPoint.proceed();
System.out.println("Logger aroundPrint afterReturning run ......");
return retVal;
} catch (Throwable e) {
System.out.println("Logger aroundPrint afterThrowing run ......");
throw e;
} finally {
System.out.println("Logger aroundPrint after run ......");
}
}
Logger aroundPrint before run ......
Logger beforePrint run ......
FinanceService 收钱 === 123.45
Logger aroundPrint afterReturning run ......
Logger aroundPrint after run ......
@After("execution(* com.linkedbear.spring.aop.b_aspectj.service.*.*(String)))")
public void afterPrint() {
System.out.println("Logger afterPrint run ......");
}
@AfterReturning("execution(* com.linkedbear.spring.aop.b_aspectj.service.*.*(String)))")
public void afterReturningPrint() {
System.out.println("Logger afterReturningPrint run ......");
}
@Pointcut("execution(* com.linkedbear.spring.aop.b_aspectj.service.*.*(String)))")
public void defaultPointcut() {
}
@After("defaultPointcut()")
public void afterPrint() {
System.out.println("Logger afterPrint run ......");
}
@AfterReturning("defaultPointcut()")
public void afterReturningPrint() {
System.out.println("Logger afterReturningPrint run ......");
}