1.Spring AOP的概念
1.1 基本概念
Spring AOP概念理解 (通俗易懂)_qukaiwei的博客-CSDN博客_aop的概念
1.2 名词概念
先上一个图,再来说一些深奥的名词。
连接点(JoinPoint):每一个方法和横切关注点相交的位置,上面有16个连接点,表示为可以被切入的位置。
切入点:只有出发在执行时考虑除数为0问题会产生异常,所以也只有这需要日志几率不,这样的连接点叫做切入点,实际切入的点。
横切关注点:
每个方法在执行的时候有4个位置可以被植入非核心业务。
切面:封装横切关注点信息的类叫做切面
通知:切面必须要完成的具体工作(增强的内容)
目标:被通知的对象
代理:向目标对应应用通知后创建的代理对
2.Spring AOP基本Demo
1.Spring 基本jar包:上一篇介绍的spring的核心jar包:context, bean,core,expression,aop
2.Spring-AOP基础jar
- spring-aspects: 传递依赖 aspectjweaver
3.Spring-AOP增强jar
- cglib
- aopalliance
- aspectjweaver
pom文件:
<?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>sh.wnxy.wn38</groupId>
<artifactId>SpringPro</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-version>5.2.5.RELEASE</spring-version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- 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.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
创建一个接口:
public interface Calc {
public int add(int i,int j);
public int sub(int i ,int j);
public int mul(int i, int j);
public int div(int i, int j);
}
创建一个实现类:
public class CalcImpl implements Calc {
@Override
public int add(int i, int j) {
return i+j;
}
@Override
public int sub(int i, int j) {
return i-j;
}
@Override
public int mul(int i, int j) {
return i*j;
}
@Override
public int div(int i, int j) {
return i/j;
}
}
创建一个切面类:
public class LogUtils {
public static void beforeWork(){
System.out.println("before work...");
}
public static void returnWork(){
System.out.println("return work...");
}
public static void throwWork(){
System.out.println("throw work...");
}
public static void finalWork(){
System.out.println("final work...");
}
}
配置文件:
<?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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="calcImpl" class="sh.wnxy.wn38.aop.CalcImpl"/>
<bean id="logUtils" class="sh.wnxy.wn38.aop.LogUtils"/>
<aop:config>
<aop:aspect ref="logUtils">
<aop:pointcut id="p1" expression="execution(* *.*(..))"/> <!-- public sh.wnxy.wn38.aop.Calc.add(int a,int b)-->
<aop:before method="beforeWork" pointcut-ref="p1"/>
<aop:after-returning method="returnWork" pointcut-ref="p1"/>
</aop:aspect>
</aop:config>
</beans>
代码执行的过程中,自动创建一个动态代理类,在w3shcool上变成了一个什么样的类讲的很清楚,在这个代码中,要注入实现类,和util类。所以在调用add()方法时,执行的时实现类的方法,另外在spring中,以JDK动态代理的方式识别到在方法执行前和返回时需要执行的操作。