注解配置
接下来的配置步骤
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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
对类添加注解
在增强类与被增强类上都添加注解@Component
在增强类上再加上@Aspect
在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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解配置-->
<context:component-scan base-package="xin.students.aopNno"/>
<!--开启注Aspect生成代理对象-->
<aop:aspectj-autoproxy/>
</beans>
运行结果
最终通知与异常通知只会执行一个
无异常时
有异常时
其它内容
execution表达式介绍
execution表达式的复用
在增强函数中任意写一个返回值为void的函数
给函数上方加上
@Pointcut(value = "execution(* xin.students.aopNno.User.add(..))") //要复用的切入点表达式
/**
* 相同的切入点"execution"表达式复用,@Before(value = "pointcutDemo()")
* 与@Before(value = "execution(* xin.students.aopNno.User.add(..))")的作用相同
* "@Before可以换成其它的",函数的返回值必须是void
*/
@Pointcut(value = "execution(* xin.students.aopNno.User.add(..))")
private void pointcutDemo() {
}
使用时与平常使用相同,只不过value后写的不是execution而是"函数名()"
设置增强类时的执行优先级
通过注解@Order(0),写在@Aspect下面实现增强类执行的优先级,越小执行优先级越高,从0开始
Maven依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xin.students</groupId>
<artifactId>Desktop</artifactId>
<version>1.0-SNAPSHOT</version>
<name>dome</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.7.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.17</version>
<exclusions>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring dao依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.17</version>
<exclusions>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-core</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.17</version>
<exclusions>
<exclusion>
<artifactId>aspectjweaver</artifactId>
<groupId>org.aspectj</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
</project>
所用到的代码
User
package xin.students.aopNno;
import org.springframework.stereotype.Component;
//被增强的类
@Component
public class User {
public void add() {
System.out.println("add..................");
/*int i = 0 / 0;*/
}
}
UserProxy
package xin.students.aopNno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
//增强类
@Component
@Aspect //生成代理对象
@Order(0) //增强类执行的优先级,越小执行优先级越高,从0开始
public class UserProxy {
/*执行顺序 环绕前通知,前置通知,执行代码,最终通知,后置通知,*/
/**
* 相同的切入点"execution"表达式复用,@Before(value = "pointcutDemo()")
* 与@Before(value = "execution(* xin.students.aopNno.User.add(..))")的作用相同
* "@Before可以换成其它的",函数的返回值必须是void
*/
@Pointcut(value = "execution(* xin.students.aopNno.User.add(..))")
private void pointcutDemo() {
}
//前置通知 @Before(value = "pointcutDemo()")
@Before(value = "execution(* xin.students.aopNno.User.add(..))")
public void before() {
System.out.println("before.....................");
}
//后置通知,方法返回结果后执行
@AfterReturning(value = "execution(* xin.students.aopNno.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning.....................");
}
//最终通知,方法结束后通知
@After(value = "execution(* xin.students.aopNno.User.add(..))")
public void after() {
System.out.println("after.....................");
}
//异常通知
@AfterThrowing(value = "execution(* xin.students.aopNno.User.add(..))")
public void afterThrowing() {
System.out.println("afterThrowing.....................");
}
//环绕通知
@Around(value = "execution(* xin.students.aopNno.User.add(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前通知.....................");
proceedingJoinPoint.proceed(); //执行被增强的方法
//当发生异常时不会执行下面的代码和后置通知,只会执行最终通知
System.out.println("环绕后通知.....................");
return null;
}
}
personProxy
package xin.students.aopNno;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(1) //增强类执行的优先级,越小执行优先级越高,从0开始
public class PersonProxy {
@Before(value = "execution(* xin.students.aopNno.User.add(..))")
public void before() {
System.out.println("personProxy before----------------------");
}
}
Main
package xin.students.aopNno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
@Test
public void test() {
ApplicationContext ac=new ClassPathXmlApplicationContext("aopNno.xml");
User user = ac.getBean("user", User.class);
user.add();
}
}