pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.siyue</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.3.5</spring.version>
</properties>
</project>
創(chuàng)建配置文件
src/main/java/com/siyue/demo/config/SpringConfig.java
package com.siyue.demo.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = {"com.siyue.demo"})
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class SpringConfig {
}
創(chuàng)建個訂單接口
src/main/java/com/siyue/demo/service/IOrderService.java
package com.siyue.demo.service;
public interface IOrderService {
void createOrder();
}
創(chuàng)建訂單實現(xiàn)類
src/main/java/com/siyue/demo/service/impl/OrderService.java
package com.siyue.demo.service.impl;
import com.siyue.demo.service.IOrderService;
import org.springframework.stereotype.Service;
@Service
public class OrderService implements IOrderService {
public OrderService() {
}
public void createOrder() {
System.out.println("創(chuàng)建訂單");
}
}
創(chuàng)建aop切面類
src/main/java/com/siyue/demo/component/AspectComponent.java
package com.siyue.demo.component;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class AspectComponent {
@Pointcut("execution(* com.siyue.demo.service.impl.*Service.*(..))")
public void pointCut() {
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
System.out.println("方法執(zhí)行前:" + joinPoint.toString());
}
@After("pointCut()")
public void after(JoinPoint joinPoint) {
System.out.println("方法執(zhí)行后:" + joinPoint.toString());
}
@AfterReturning(pointcut = "pointCut()", returning = "returnVal")
public void afterReturning(JoinPoint joinPoint, Object returnVal) {
System.out.println("方法返回前, 返回結(jié)果:"
+ returnVal + joinPoint.toString());
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object proceed;
System.out.println("環(huán)繞前");
try {
proceed = pjp.proceed();
} catch (Throwable ex) {
System.out.println("環(huán)繞異常");
throw ex;
}
System.out.println("環(huán)繞后");
return proceed;
}
@AfterThrowing(pointcut = "pointCut()", throwing = "error")
public void afterThrowing(JoinPoint joinPoint, Throwable error) {
System.out.println("方法拋出異常:" + error + joinPoint.toString());
}
}
創(chuàng)建啟動入口
src/main/java/com/siyue/demo/Main.java
package com.siyue.demo;
import com.siyue.demo.config.SpringConfig;
import com.siyue.demo.service.IOrderService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
IOrderService orderService = ctx.getBean(IOrderService.class);
orderService.createOrder();
}
}
執(zhí)行結(jié)果
環(huán)繞前
方法執(zhí)行前:execution(void com.siyue.demo.service.impl.OrderService.createOrder())
創(chuàng)建訂單
方法返回前, 返回結(jié)果:nullexecution(void com.siyue.demo.service.impl.OrderService.createOrder())
方法執(zhí)行后:execution(void com.siyue.demo.service.impl.OrderService.createOrder())
環(huán)繞后