作者:黑鸽子 | 来源:互联网 | 2023-06-08 09:35
在Spring的aop命名空间中,提供了多个元素用来在XML中声明切面:在XML文件中配置aop命名空间:<beansxmlnshttp:www.springfram
在Spring的aop命名空间中,提供了多个元素用来在XML中声明切面:
在XML文件中配置aop命名空间:
"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:
http:
http:
http:
http:
一、声明前置和后置通知
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 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>元素内,可以声明一个或多个通知器、切面或切点。
对于上面重复的切点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(){
Encorable perEncore=(Encorable)perform;
perEncore.performEncore();
}