基于xml的AOP开发
1.快速入门
- 导入AOP的相关坐标
- 创建目标接口和目标类(内部有切点)
- 创建切面类(内部有增强的方法)
- 在目标类和切面类的对象创建权交给spring
- 在applicationContext.xml中配置织入关系
- 测试代码
配置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>org.example</groupId>
<artifactId>Spring_AOP</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<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.9.8.RC3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
创建工程包com.AOP,将jdk包下的Target和TargetInterface粘贴过来,在创建文件myAspect
package com.AOP;
public class myAspect {
public void before(){
System.out.println("前置增强");
}
}
在resource下创建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="target" class="com.AOP.Target"></bean>
<!--切面对象-->
<bean id="myAspect" class="com.AOP.myAspect"></bean>
<!--配置织入:告诉spring框架,哪些方法(切点)需要进行那些增强(前置、后置)-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--切面:切点+通知-->
<aop:before method="before" pointcut="execution(public void com.AOP.Target.save())"></aop:before>
</aop:aspect>
</aop:config>
</beans>
创建测试文件,在AOP包下创建文件AopTest
package com.AOP;
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:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.save();
}
}
运行test1方法即可
2.xml配置AOP详解
2.1.切点表达式写法
2.2.通知类型
修改myAspect文件
package com.AOP;
import org.aspectj.lang.ProceedingJoinPoint;
public class myAspect {
public void before(){
System.out.println("前置增强");
}
public void afterReturning(){
System.out.println("后置增强");
}
//ProceedingJoinPoint:正在执行的连接点==切点
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强");
Object proceed = pjp.proceed();//切点方法
System.out.println("环绕后增强");
return proceed;
}
}
配置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="target" class="com.AOP.Target"></bean>
<!--切面对象-->
<bean id="myAspect" class="com.AOP.myAspect"></bean>
<!--配置织入:告诉spring框架,哪些方法(切点)需要进行那些增强(前置、后置)-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--切面:切点+通知-->
<aop:before method="before" pointcut="execution(public void com.AOP.Target.save())"></aop:before>
<aop:after-returning method="afterReturning" pointcut="execution(public void com.AOP.Target.save())"></aop:after-returning>
<aop:around method="around" pointcut="execution(public void com.AOP.Target.save())"></aop:around>
</aop:aspect>
</aop:config>
</beans>
运行test1
2.3.切点表达式的抽取
修改applicationContext配置
<?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.AOP.Target"></bean>
<!--切面对象-->
<bean id="myAspect" class="com.AOP.myAspect"></bean>
<!--配置织入:告诉spring框架,哪些方法(切点)需要进行那些增强(前置、后置)-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<!--抽取切点表达式-->
<aop:pointcut id="myPoint" expression="execution(* com.AOP.*.*(..))"/>
<aop:around method="around" pointcut-ref="myPoint"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPoint"/>
</aop:aspect>
</aop:config>
</beans>
运行test1
3.知识要点