Spring:基于xml配置的AOP程序开发
以基于JDK的动态代理为例(即都会有一个接口)
实现步骤:
1、依赖导入:AOP织入依赖 aspectjweaver 第三方的框架,spring-aop组件中引入了它
2、定义目标接口
3、定义目标对象类
4、定义切面类
5、xml配置 以配置取代代码
6、编写测试代码
1、依赖导入:AOP织入依赖 aspectjweaver
<!--=====AOP Weaving=====-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
Spring上下文依赖也要有:spring-context
2、定义目标接口
public interface TargetInterface {
void save();
}
3、定义目标对象类
public class Target implements TargetInterface{
public void save(){
System.out.println("Target:save() running........");
}
}
4、定义切面类
Spring框架提供了5种增强类型
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;
}
public void afterThrowing(){
System.out.println("异常抛出增强");
}
//-------------最终增强------------------
/*
无论怎样,都会执行的增强。
“无论怎样”是指如果程序出现异常,有的增强方式可能就执行不到,但最终增强一定会执行。
这有点像try-catch-finally中的finally块中的代码,是一定会被执行的。
*/
public void after(){
System.out.println("最终增强");
}
}
5、xml配置 以配置取代代码
applicationContext.xml中配置
需要配置的内容:
(1)目标对象类与切面类的Bean对象
对象交由IoC容器创建
(2)aop织入配置 <aop:config>
告诉Spring框架 哪些方法(切点)需要进行哪些增强(前置、后置…)
这里需要引入命名空间“aop”
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="cn.leap.aop_xml.Target"/>
<!--切面对象-->
<bean id="myAspect" class="cn.leap.aop_xml.MyAspect"/>
<!--配置织入:告诉Spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...)-->
<aop:config>
<!--========切面: 切点+增强========
pointcut:切点表达式
语法格式:execution([权限修饰符] 返回类型 包名.类名.方法名(参数))
-->
<aop:aspect ref="myAspect">
<!--抽取切点表达式-->
<aop:pointcut id="myPointcut" expression="execution(* cn.leap.aop_xml.*.*(..))"/>
<!--最终增强-->
<aop:after method="after" pointcut-ref="myPointcut"/>
<!--环绕增强-->
<aop:around method="around" pointcut-ref="myPointcut"/>
<!--后置增强-->
<aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/>
<!--前置增强-->
<aop:before method="before" pointcut-ref="myPointcut"/>
<!--异常抛出增强-->
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
注意:
xml配置增强时,Spring框架如何决定各种增强的执行顺序呢?
注解配置时呢?
需要去实测测! 不过,据说实际应用中,很少对一个目标方法施加多种增强
6、编写测试代码
maven项目的测试包下写,使用SpringJunit来进行测试。
/**
* Desc: SpringJunit测试--AOP XML配置测试
*/
@RunWith(SpringJUnit4ClassRunner.class) //SpringJunit中指定新的运行器,由它来创建IoC窗口
@ContextConfiguration("classpath:applicationContext.xml")//SpringJunit中,指定配置文件
public class AopXMLTest {
/*
目标方法save()方法是目标对象Target类中的方法。
若不用AOP对其增强,可以写成private Target target 或 private TargetInterface target
这只是普通的方法调用。
若使用AOP对其增强,必须是private TargetInterface target
原因:
JDK动态代理模式中,代理对象与目标对象Target实现同一个接口,两者是兄弟关系。
AOP中,代理对象是由IoC容器创建的。
*/
@Autowired
private TargetInterface target;//这个其实是代理对象
@Test
public void test01() {
target.save();
}
}
控制台打印内容:
环绕前增强
前置增强
Target:save() running........
后置增强
环绕后增强
最终增强