AOP的术语
1、连接点(join point)
所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(需要增强的方法)
2、通知(Advice)
所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知(用来增强其他方法的方法)
3、切面(Aspect)
是切入点和通知(引介)的结合。
4、切点(PointCut)
切入点是指我们要对哪些 Joinpoint 进行拦截
5、织入(Weaving)
是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入
使用XML配置的方式快速入门Spring AOP
(1)引入需要的依赖,需要引入的又spring-context、用于测试的spring-test和junit、还有一个aspectjweaver。虽然spring中又自带的spring-aop,但是由于aspectjweaver比spring-aop自带的好用,spring也推荐使用aspectjweaver
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
(2)导入包以后创建目标接口和目标类。创建接口,该接口有个保存的方法。创建该接口的实现类Target并实现save()方法
package com.spring.aop;
public interface TargetInterface {
public void save();
}
package com.spring.aop;
public class Target implements TargetInterface{
public void save()
{
System.out.println("save running......");
}
}
(3)创建切面类(该类中的方法用于增强目标类)。MyAspect中创建了方法before(),该方法多为前置增强方法,也就是在运行目标类的方法之前先运行该方法。
package com.spring.aop;
public class MyAspect {
public void before()
{
System.out.println("前置增强......");
}
}
(4)将目标类和切面类装入Spring Ioc容器中。
<?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="com.spring.aop.Target"></bean>
<!--切面对象-->
<bean id="myAspect" class="com.spring.aop.MyAspect"></bean>
</beans>
(5)在上述的XML文件中,配置织入关系,即将切点,连接点,切面等信息进行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="target" class="com.spring.aop.Target"></bean>
<!--切面对象-->
<bean id="myAspect" class="com.spring.aop.MyAspect"></bean>
<!--配置织入:告诉spring框架那些方法(切点)需要进行哪些增强-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--切点+通知-->
<aop:before method="before" pointcut="execution(public void com.spring.aop.Target.save())"/>
</aop:aspect>
</aop:config>
</beans>
该段代码需要详细解释:
1、创建aop作用域
模仿xmlns="http://www.springframework.org/schema/beans"的方式添加aop的作用域,代码如下:xmlns:aop="http://www.springframework.org/schema/aop",即将beans改为aop,在xmlns前加入:aop。
在 xsi:schemaLocation中模仿http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd的方式再加入两条aop的,即
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd。将beans都改为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">
2、配置织入
在标识<aop:config></aop:config>中开始配置
声明切面:<aop:aspect ref="myAspect"></aop:aspect> 该段表明将Ioc中的myAspect引用作为切面,在该标识对里面再进行更详细的配置。以前置增强为例,前置增强标识为<aop:before></aop:before> 其中配置项有method,method的值为字符串,即拿来增强的方法(切面类中的前置增强方法)另一个配置项pointcut,pointcut即切点(需要增强的目标方法)pointcut的值为切点表达式,表达式语法 execution([修饰符]返回值类型 包名.类名.方法名(参数))。
配置好的代码如下。目标方法是com.spring.aop.Target.save()方法,前置增强方法为myAspect类中的before
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--切点+通知-->
<aop:before method="before" pointcut="execution(public void com.spring.aop.Target.save())"/>
</aop:aspect>
</aop:config>
(6)编写测试代码
import com.spring.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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationConfig.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1()
{
target.save();
}
}
对象target调用save方法,运行结果如下
可以看到前置增强方法也运行了。