一、基于xml的AOP开发
1. 快速入门
开发步骤:
①:导入AOP相关坐标
②:创建目标接口和目标类(内部有切点)
③:创建切面类(内部有增强方法)
④:将目标类和切面类的对象创建权交给spring
⑤:在applicationContext.xml中配置织入关系
⑥:测试代码
①:在pom.xml中导入AOP相关坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
②:创建目标接口和目标类(内部有切点)
目标接口
package aop;
/**
* @create 2021-11-05 16:45
* 目标接口
*/
public interface TargetInterface {
void save();
}
目标类
package aop;
/**
* @create 2021-11-05 16:46
* 需要增强的目标类
*/
public class Target implements TargetInterface {
@Override
public void save() {
System.out.println("save running......");
}
}
③:创建切面类(内部有增强方法)
package aop;
/**
* @create 2021-11-05 17:58
* 切面类
*/
public class MyAspect {
public void before(){
System.out.println("使用AOP前置增强");
}
}
④:将目标类和切面类的对象创建权交给spring(在applicationContext.xml文件中配置bean)
<!-- 目标对象 -->
<bean id="target" class="aop.Target"></bean>
<!-- 切面对象 -->
<bean id="myAspect" class="aop.MyAspect"></bean>
⑤:在applicationContext.xml中配置织入关系
配置织入关系前需要添加aop命名空间,完整配置如下:
<?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="target" class="aop.Target"></bean>
<!-- 切面对象 -->
<bean id="myAspect" class="aop.MyAspect"></bean>
<!-- 配置织入,告诉Spring框架 哪些方法(切点)需要进行哪些增强(前置、后置。。。) -->
<aop:config>
<!-- 声明切面 -->
<aop:aspect ref="myAspect">
<!-- 切面:切点+通知 -->
<aop:before method="before" pointcut="execution(public void aop.Target.save())"></aop:before>
</aop:aspect>
</aop:config>
</beans>
⑥:测试代码(通过JUnit进行测试,需要在pom.xml中导入junit坐标和spring-test坐标)
坐标导入:
<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>5.0.5.RELEASE</version>
</dependency>
测试代码:
package test;
import aop.TargetInterface;
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.SpringJUnit4ClassRunner;
/**
* @create 2021-11-05 18:15
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target; //利用注解方式注入target对象
@Test
public void test1(){
target.save();
}
}
控制台显示效果:
切入点表达式的使用规则:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
有“?”号的部分表示可省略的,modifers-pattern表示修饰符如public、protected等,ret-type-pattern表示方法返回类型,declaring-type-pattern代表特定的类,name-pattern代表方法名称,param-pattern表示参数,throws-pattern表示抛出的异常。在切入点表达式中,可以使用*来代表任意字符,用…来表示任意个参数。
星号后面要有个空格