This commit is contained in:
罗祥 2018-12-28 18:01:03 +08:00
parent 38eebc98ae
commit 3cda29d3b9
16 changed files with 575 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?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.heibaiying</groupId>
<artifactId>spring-aop</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring-base-version>5.1.3.RELEASE</spring-base-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-base-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-base-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-base-version}</version>
</dependency>
<!--aop 依赖包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-base-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!--单元测试相关包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-base-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,61 @@
package com.heibaiying.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* @author : heibaiying
* @description : 自定义切面
*/
@Aspect
@Component //除了加上@Aspect外 还需要声明为spring的组件 @Aspect只是一个切面声明
public class CustomAdvice {
/**
* 使用 || , or 表示或
* 使用 && , and 表示与
* ! 表示非
*/
@Pointcut("execution(* com.heibaiying.service.OrderService.*(..)) && !execution(* com.heibaiying.service.OrderService.deleteOrder(..))")
private void pointCut() {
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
//获取节点名称
String name = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println(name + "方法调用前:获取调用参数" + Arrays.toString(args));
}
@AfterReturning(pointcut = "pointCut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
System.out.println("后置返回通知结果" + result);
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知-前");
//调用目标方法
Object proceed = joinPoint.proceed();
System.out.println("环绕通知-后");
return proceed;
}
@AfterThrowing(pointcut = "pointCut()", throwing = "exception")
public void afterThrowing(JoinPoint joinPoint, Exception exception) {
System.err.println("后置异常通知:" + exception);
}
@After("pointCut()")
public void after(JoinPoint joinPoint) {
System.out.println("后置通知");
}
}

View File

@ -0,0 +1,50 @@
package com.heibaiying.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* @author : heibaiying
* @description : 自定义切面
*/
@Order(1)
@Aspect
@Component
public class FirstAdvice {
@Pointcut("execution(* com.heibaiying.service.OrderService.deleteOrder(..)))")
private void pointCut() {
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
System.out.println("第一个切面的before");
}
@AfterReturning(pointcut = "pointCut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
System.out.println("第一个切面的AfterReturning");
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("第一个切面环绕通知-前");
//调用目标方法
Object proceed = joinPoint.proceed();
System.out.println("第一个切面环绕通知-后");
return proceed;
}
@After("pointCut()")
public void after(JoinPoint joinPoint) {
System.out.println("第一个切面后置通知");
}
}

View File

@ -0,0 +1,48 @@
package com.heibaiying.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author : heibaiying
* @description : 自定义切面
*/
@Order(1)
@Aspect
@Component
public class SecondAdvice {
@Pointcut("execution(* com.heibaiying.service.OrderService.deleteOrder(..)))")
private void pointCut() {
}
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
System.out.println("第二个切面的before");
}
@AfterReturning(pointcut = "pointCut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
System.out.println("第二个切面的AfterReturning");
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("第二个切面环绕通知-前");
//调用目标方法
Object proceed = joinPoint.proceed();
System.out.println("第二个切面环绕通知-后");
return proceed;
}
@After("pointCut()")
public void after(JoinPoint joinPoint) {
System.out.println("第二个切面后置通知");
}
}

View File

@ -0,0 +1,24 @@
package com.heibaiying.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @author : heibaiying
* @description : 订单实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private long id;
private String productName;
private Date orderTime;
}

View File

@ -0,0 +1,15 @@
package com.heibaiying.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @author : heibaiying
* @description : 开启切面配置
*/
@Configuration
@ComponentScan("com.heibaiying.*")
@EnableAspectJAutoProxy // 开启@Aspect注解支持 等价于<aop:aspectj-autoproxy>
public class AopConfig {
}

View File

@ -0,0 +1,16 @@
package com.heibaiying.service;
import com.heibaiying.bean.Order;
/**
* @author : heibaiying
* @description : 订单查询接口
*/
public interface OrderService {
Order queryOrder(Long id);
Order createOrder(Long id, String productName);
boolean deleteOrder(Long id);
}

View File

@ -0,0 +1,28 @@
package com.heibaiying.service;
import com.heibaiying.bean.Order;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
* @author : heibaiying
* @description : 订单查询接口实现类
*/
@Service
public class OrderServiceImpl implements OrderService {
public Order queryOrder(Long id) {
return new Order(id, "product", new Date());
}
public Order createOrder(Long id, String productName) {
// 模拟抛出异常
// int j = 1 / 0;
return new Order(id, "new Product", new Date());
}
public boolean deleteOrder(Long id) {
return true;
}
}

View File

@ -0,0 +1,35 @@
package com.heibaiying.aop;
import com.heibaiying.config.AopConfig;
import com.heibaiying.service.OrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author : heibaiying
* @description : 关于多个切面在同一个切入点上执行顺序的例子 可以在spring-aop-annotation 中查看
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AopConfig.class)
public class AopTest {
@Autowired
private OrderService orderService;
@Test
public void saveAndQuery() {
orderService.createOrder(1283929319L, "手机");
//orderService.queryOrder(4891894129L);
}
@Test
public void delete() {
orderService.deleteOrder(12793179L);
}
}

63
spring/spring-aop/pom.xml Normal file
View File

@ -0,0 +1,63 @@
<?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.heibaiying</groupId>
<artifactId>spring-aop</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring-base-version>5.1.3.RELEASE</spring-base-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-base-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-base-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-base-version}</version>
</dependency>
<!--aop 相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-base-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<!--单元测试相关包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-base-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,48 @@
package com.heibaiying.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;
/**
* @author : heibaiying
* @description : 自定义切面
*/
public class CustomAdvice {
//前置通知
public void before(JoinPoint joinPoint) {
//获取节点名称
String name = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println(name + "方法调用前:获取调用参数" + Arrays.toString(args));
}
//后置通知(抛出异常后不会被执行)
public void afterReturning(JoinPoint joinPoint, Object result) {
System.out.println("后置返回通知结果" + result);
}
//环绕通知
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕通知-前");
//调用目标方法
Object proceed = joinPoint.proceed();
System.out.println("环绕通知-后");
return proceed;
}
//异常通知
public void afterException(JoinPoint joinPoint, Exception exception) {
System.err.println("后置异常通知:" + exception);
}
;
// 后置通知 总会执行 但是不能访问到返回值
public void after(JoinPoint joinPoint) {
System.out.println("后置通知");
}
}

View File

@ -0,0 +1,24 @@
package com.heibaiying.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @author : heibaiying
* @description : 订单实体类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private long id;
private String productName;
private Date orderTime;
}

View File

@ -0,0 +1,16 @@
package com.heibaiying.service;
import com.heibaiying.bean.Order;
import java.util.Date;
/**
* @author : heibaiying
* @description :
*/
public interface OrderService {
Order queryOrder(Long id);
Order createOrder(Long id, String productName);
}

View File

@ -0,0 +1,22 @@
package com.heibaiying.service;
import com.heibaiying.bean.Order;
import java.util.Date;
/**
* @author : heibaiying
* @description :
*/
public class OrderServiceImpl implements OrderService {
public Order queryOrder(Long id) {
return new Order(id, "product", new Date());
}
public Order createOrder(Long id, String productName) {
// 模拟抛出异常
// int j = 1 / 0;
return new Order(id, "new Product", new Date());
}
}

View File

@ -0,0 +1,35 @@
<?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">
<!--开启后允许使用Spring AOP的@AspectJ注解 如果是纯xml配置 可以不用开启这个声明-->
<aop:aspectj-autoproxy/>
<!-- 1.配置目标对象 -->
<bean name="orderService" class="com.heibaiying.service.OrderServiceImpl"/>
<!-- 2.声明切面 -->
<bean name="myAdvice" class="com.heibaiying.advice.CustomAdvice"/>
<!-- 3.配置将通知织入目标对象 -->
<aop:config>
<!--命名切入点 关于切入点更多表达式写法可以参见README.md-->
<aop:pointcut expression="execution(* com.heibaiying.service.OrderService.*(..))" id="cutPoint"/>
<aop:aspect ref="myAdvice">
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="cutPoint"/>
<!-- 后置通知 如果需要拿到返回值 则要指明返回值对应的参数名称-->
<aop:after-returning method="afterReturning" pointcut-ref="cutPoint" returning="result"/>
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="cutPoint"/>
<!-- 后置异常 如果需要拿到异常 则要指明异常对应的参数名称 -->
<aop:after-throwing method="afterException" pointcut-ref="cutPoint" throwing="exception"/>
<!-- 最终通知 -->
<aop:after method="after" pointcut-ref="cutPoint"/>
</aop:aspect>
</aop:config>
</beans>

View File

@ -0,0 +1,27 @@
package com.heibaiying.aop;
import com.heibaiying.service.OrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author : heibaiying
* @description : 关于多个切面在同一个切入点上执行顺序的例子 可以在spring-aop-annotation 中查看
*/
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:aop.xml")
public class AopTest {
@Autowired
private OrderService orderService;
@Test
public void save() {
orderService.createOrder(1283929319L, "手机");
//orderService.queryOrder(4891894129L);
}
}