前言
Spring AOP原理不清楚的,可先看上一篇博客:Spring AOP之概念篇
一、JavaConfig方式创建切面
1、首先看下项目目录结构
2、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.scb</groupId>
<artifactId>springaop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springaop</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<!--aspectj依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.10</version>
</dependency>
<!--spring相关包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、创建Performance接口与其实现类Theatre
package com.scb.springaop.aop;
public interface Performance {
void perform();
}
package com.scb.springaop.aop;
public class Theatre implements Performance{
@Override
public void perform(){
System.out.println("50个人正在表演舞台剧");
}
}
将其声明为Bean
package com.scb.springaop.aop;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy //启用Aspectj自动代理
public class ConcertConfig {
@Bean //声明了一个切面bean
public Audience audience(){
return new Audience();
}
@Bean //接口注入
public Performance theatre(){
return new Theatre();
}
}
4、创建Audience切面类
package com.scb.springaop.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect //表明该类别是一个切面
public class Audience {
@Pointcut("execution(* com.scb.springaop.aop.Performance.perform(..))") //定义命令的切点
public void performance(){}
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint jp){
try{
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP!!!");
}catch (Throwable e){
System.out.println("Demanding a refund");
}
}
@Before("performance()") //在表演之前
public void silenceCellPhones(){
System.out.println("before:Silencing cell phones");
}
@Before("performance()") //在表演之前
public void takeSeats(){
System.out.println("before:Taking seats");
}
@AfterReturning("performance()") //表演之后
public void applause(){
System.out.println("AfterReturning:CLAP CLAP CLAP!!!");
}
@AfterThrowing("performance()")
public void demandRefund(){ //表演失败之后
System.out.println("AfterThrowing:Demanding a refund");
}
@After("performance()")
public void leave(){
System.out.println("After:leave");
}
}
5、创建测试类PerformanceTest
package com.scb.springaop.aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class PerformanceTest {
@Test
public void test(){
//加载java配置类获取Spring应用上下文
ApplicationContext ac=new AnnotationConfigApplicationContext(ConcertConfig.class);
Performance pf=ac.getBean(Performance.class);
pf.perform();
}
}
输出结果为:
从运行结果可看出通知的执行顺序为:Around --> Before --> 目标方法 --> Around --> After-returning/ After-throwing --> After
二、以XML方式创建切面
以XML方式创建切面时,只需修改Audience切面类,将其注解去掉,变成一个POJO类,在创建一个applicationContext.xml进行配置即可。
<?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">
<bean id="audience" class="com.scb.springaop.aop.Audience"></bean>
<bean id="theatre" class="com.scb.springaop.aop.Theatre"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* com.scb.springaop.aop.Performance.perform(..))"/>
<aop:around method="watchPerformance" pointcut-ref="performance"></aop:around>
<aop:before method="silenceCellPhones" pointcut-ref="performance"></aop:before>
<aop:before method="takeSeats" pointcut-ref="performance"></aop:before>
<aop:after-returning method="applause" pointcut-ref="performance"></aop:after-returning>
<aop:after-throwing method="demandRefund" pointcut-ref="performance"></aop:after-throwing>
<aop:after method="leave" pointcut-ref="performance"></aop:after>
</aop:aspect>
</aop:config>
</beans>
创建测试类从XML中获取Bean进行测试
package com.scb.springaop.aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPTest {
@Test
public void test(){
//加载XML配置类获取Spring应用上下文
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Performance performance=(Performance)applicationContext.getBean("theatre");
performance.perform();
}
}
输出结果为:
三、AspectJ使用示例
package com.scb.springaop.aspectj;
public class Hello {
public void sayHello() {
System.out.println("Hello, AspectJ!");
}
public static void main(String[] args) {
Hello hello = new Hello();
hello.sayHello();
}
}
创建AspectJ切面
package com.scb.springaop.aspectj;
public aspect TxAspect {
void around():call(void Hello.sayHello()){
System.out.println("开始事务...");
proceed();
System.out.println("事务结束...");
}
}
运行Hello类的main方法