首页 > 代码库 > Spring 之定义切面尝试(基于 XML)
Spring 之定义切面尝试(基于 XML)
有些场景下只能基于 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(); }}
一切正常。。。
Spring 之定义切面尝试(基于 XML)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。