在Spring的aop命名空间中,提供了多个元素用来在XML中声明切面:
在XML文件中配置aop命名空间:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
一、声明前置和后置通知
1、定义一个普通的POJO作为通知(增强advice)
public class Audience {
public void silenceCellphone(){
System.out.println("please silence cell phone");
}
public void takeSeates(){
System.out.println("please take Seate");
}
public void applause(){
System.out.println("CLAP CLAP CLAP");
}
public void demandRefund(){
System.out.println("Refund refund");
}
}
2、在XML中声明前置通知和后置通知
<!--将通知定义为一个bean,也可以不用明确定义,采用注解方式-->
<bean id="audience" class="org.aop.Audience"/>
<aop:config>
<!--定义一个切面-->
<aop:aspect ref="audience">
<aop:before pointcut="execution(* org.aop.Perform.play())" method="silenceCellphone"/>
<aop:before pointcut="execution(* org.aop.Perform.play())" method="takeSeates"/>
<aop:after method="applause" pointcut="execution(* org.aop.Perform.play())"/>
<aop:after-throwing method="demandRefund" pointcut="execution(* org.aop.Perform.play())"/>
</aop:aspect>
</aop:config>
在<\aop:config>元素内,可以声明一个或多个通知器、切面或切点。
它拥有一个proxy-target-class属性,当设置为true时,表示其中声明的切面均使用CGLib动态代理技术,当设置为false时,使用Java动态代理技术。
对于上面重复的切点pointcut属性值,可以进行优化:
<aop:config>
<!--定义一个切面-->
<aop:aspect ref="audience">
<!--定义一个共同的切点-->
<aop:pointcut id="point" expression="execution(* org.aop.Perform.play())"/>
<!--引用切点-->
<aop:before pointcut-ref="point" method="silenceCellphone"/>
<aop:before pointcut-ref="point" method="takeSeates"/>
<aop:after method="applause" pointcut-ref="point"/>
<aop:after-throwing method="demandRefund" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
如果想让定义的切点可以在多个切面使用,可以把<\aop:pointcut>元素放在<\aop:config>元素的范围内。
3、目标对象
@Component
public class Perform implements Performence {
public void play(){
System.out.println("play what");
}
}
4、测试一下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-aop.xml"})
public class CDPConfigTest {
@Autowired
private Performence perform;
@Test
public void doP(){
perform.play();
}
}
//结果:
please silence cell phone
please take Seate
play what
CLAP CLAP CLAP
二、声明环绕通知
1、定义一个新的环绕通知
@Component
public class AroundAudience {
public void watchPerformance(ProceedingJoinPoint jp){
try{
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
//执行被通知的方法
jp.proceed();
System.out.println("CLAP CLAP CALP");
}catch (Throwable e){
System.out.println("Demanding a refund");
}
}
}
2、在XML中声明环绕通知
<aop:config>
<aop:aspect ref="aroundAudience">
<aop:pointcut id="aroundPoint" expression="execution(* org.aop.Perform.aroundPlay())"/>
<aop:around method="watchPerformance" pointcut-ref="aroundPoint"/>
</aop:aspect>
</aop:config>
3、测试一下
@Autowired
private Performence perform;
@Test
public void doP(){
//类型如果是接口,那方法一定要在接口中有定义
perform.aroundPlay();
}
三、为通知传递参数
传递到目标对象中的某个方法的参数,也可以传递到通知中。
通知中与目标对象方法中的参数名保持一致。并且,在通知中对参数的修改不会改变目标对象的方法参数。
1、一个通知
public void countTrack(int Num){
Num++;
System.out.println("通知中 trackNum="+Num);
}
2、XML中配置
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="trackplay" expression="execution(* org.aop.Perform.palyTrace(int)) and args(Num)"/>
<aop:before method="countTrack" pointcut-ref="trackplay"/>
</aop:aspect>
</aop:config>
3、目标对象
public void palyTrace(int Num){
System.out.println("目标对象 trackNum="+Num);
}
4、测试一下
@Test
public void doP(){
//类型如果是接口,那方法一定要在接口中有定义
perform.palyTrace(8);
}
//结果
通知中 trackNum=9
目标对象 trackNum=8
四、通过切面引入新的功能
1、新功能所在的接口(要引入的)和实现类
//新功能接口
public interface Encorable {
void performEncore();
}
//新更能实现类
@Component
public class defaultEncore implements Encorable {
public void performEncore(){
System.out.print("这是新引入的功能");
}
}
2、XML中的配置
<aop:config>
<aop:aspect>
<aop:declare-parents types-matching="org.aop.Performence+"
implement-interface="org.aop.Encorable"
default-impl="org.aop.defaultEncore"/>
</aop:aspect>
</aop:config>
types-matching:Performence接口所实现的子类,也就是对应的目标切点类。
implement-interface:新加入的接口类。
default-impl:新加入的接口的默认实现类。
也可以修改为:
<aop:config>
<aop:aspect>
<aop:declare-parents types-matching="org.aop.Performence+"
implement-interface="org.aop.Encorable"
delegate-ref="defaultEncore"/>
</aop:aspect>
</aop:config>
<bean id="defaultEncore" class="org.aop.defaultEncore"/>
4、测试一下
@Autowired
private Performence perform;
@Test
public void doP(){
//就像perform实现了Encorable接口一样
Encorable perEncore=(Encorable)perform;
perEncore.performEncore();
}
Advisor的配置
<\aop:advisor advice-ref=”” pointcut-ref=”“/>
它是切点和通知的复合体,仅包含一个切点和一个通知。它的切点表示方法和<\aop:aspect>相同,增强定义方式则要实现相关的接口(MethodBeforeAdvice、AfterReturningAdvice/ThrowsAdvice)。
<\aop:config>元素中的<\aop:pointcut>、<\aop:advisor>、<\aop:aspect>三者的配置顺序:
首先是<\aop:pointcut>,其次是<\aop:advisor>,最后是<\aop:aspect>。
而在<\aop:aspect>中定义的<\aop:pointcut>则没有先后顺序要求,可以在任何位置定义。