Spring 之定义切面尝试(基于 XML)

时间:2021-04-09 20:29:28

有些场景下只能基于 XML 来定义切面。

【Spring 之定义切面尝试】

1、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: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">

<!-- 启用 Aspectj 自动代理 不启动也能用???-->
<aop:aspectj-autoproxy />

<bean id="audience" class="concert.Audience" />

<aop:config>
<aop:aspect ref="audience">
<aop:before method="silenceCellPhones"
pointcut
="execution(* concert.Performance.perform(..))" />
<aop:before method="takeSeats"
pointcut
="execution(* concert.Performance.perform(..))" />
<aop:after-returning method="applause"
pointcut
="execution(* concert.Performance.perform(..))" />
<aop:after-throwing method="demandRefund"
pointcut
="execution(* concert.Performance.perform(..))" />
</aop:aspect>
</aop:config>

<bean id="theShow" class="concert.TheShow" />

</beans>

使用 <aop:pointcut> 定义命名切点

<?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: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">

<!-- 启用 Aspectj 自动代理 不启动也能用???-->
<aop:aspectj-autoproxy />

<bean id="audience" class="concert.Audience" />

<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* concert.Performance.perform(..))" />
<aop:before method="silenceCellPhones"
pointcut-ref
="performance" />
<aop:before method="takeSeats"
pointcut-ref
="performance" />
<aop:after-returning method="applause"
pointcut-ref
="performance" />
<aop:after-throwing method="demandRefund"
pointcut-ref
="performance" />
</aop:aspect>
</aop:config>

<bean id="theShow" class="concert.TheShow" />

</beans>

修改为环绕通知:

package concert;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

public class Audience {

public void performance() {}

public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println(
"Silencing cell phones");
System.out.println(
"Taking seats");
jp.proceed();
System.out.println(
"CLAP CLAP CLAP!!!AP CLAP!!!AP CLAP!!!AP CLAP!!!");
}
catch (Throwable e) {
System.out.println(
"Demanding a refund");
}
}
}
<?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: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">

<aop:aspectj-autoproxy />

<bean id="audience" class="concert.Audience" />

<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* concert.Performance.perform(..))" />

<aop:around method="watchPerformance"
pointcut-ref
="performance" />
</aop:aspect>
</aop:config>

<bean id="theShow" class="concert.TheShow" />

</beans>

 

2、测试所定义的切面

package concert;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
public static void main(String[] args) {
// 导入配置
ApplicationContext ctx = new ClassPathXmlApplicationContext("concert-config.xml");

Performance performance
= (Performance) ctx.getBean("theShow");
performance.perform();
}
}

 一切正常。。。