Spring AOP概念学习(四)-基于schema的aop支持
看了文档,基于@JAspect支持的Spring AOP不知什么原因,编写相关的例子程序,没有运行成功,所以暂时不能写出来供大家交流,如果调试出来后,一定补上。现在先来看看Spring中另一种AOP的支持-基于Schema的AOP支持。
和以前一样,还是先把例子程序写出来,有什么注意的地方会在稍后注明。
1、通知
package com.javaeye.sunjiesh.aop.aspect;public class AspectExample {/** * 前置通知(Before advice) */public void beforeAdvice() {System.out.println("beforeAdvice");}/** * 带参数的前置通知(Before advice) * @param parameter */public void beforeAdvice2(String parameter){System.out.println("beforeAdvice2");System.out.println("parameter is "+parameter);}/** * 后通知(After (finally) advice) */public void afterAdvice() {System.out.println("afterAdvice");}/** * 返回后通知(After returning advice) * @param parameter 连接点运行后返回的参数 */public void afterReturnAdvice(String parameter) {System.out.println("afterReturnAdvice");System.out.println("parameter is "+parameter);}/** * 抛出异常后通知(After throwing advice) * @param ex 连接点运行时发生的异常 */public void afterThrowingAdvice(Exception ex) {System.out.println("afterThrowingAdvice");System.out.print(ex.getMessage());}/** * 环绕通知(Around Advice) */public void aroundAdvice() {System.out.println("aroundAdvice");}}
2、连接点
package com.javaeye.sunjiesh.aop.aspect;public class ServiceExample {public void adviceTest(){System.out.println("adviceTest is run");}public String adviceTest2(){System.out.println("adviceTest2 is run");try {throw new Exception();} catch (Exception e) {// TODO Auto-generated catch block//e.printStackTrace();System.out.println(e.getMessage());}finally{return null;}}}
3、spring配置文件
<?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" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><bean id="aspectExample" ref="aspectExample"><aop:pointcut id="myPointcut"expression="execution(* com.javaeye.sunjiesh.aop.aspect.ServiceExample.*(..))" /><aop:before method="beforeAdvice" pointcut-ref="myPointcut" /><aop:after method="afterAdvice" pointcut-ref="myPointcut" /><aop:after-returning method="afterReturnAdvice"returning="parameter" pointcut-ref="myPointcut" /><aop:after-throwing method="afterThrowingAdvice"throwing="ex" pointcut-ref="myPointcut" /></aop:aspect></aop:config><aop:config><aop:aspect id="myAspect2" ref="aspectExample"><aop:pointcut id="myPointcut2"expression="execution(* com.javaeye.sunjiesh.aop.aspect.ServiceExample2.adviceTest(String)) and args(parameter)" /><aop:before method="beforeAdvice2" pointcut-ref="myPointcut2" /></aop:aspect></aop:config></beans>
4、主函数
package com.javaeye.sunjiesh.aop.aspect;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AspectMain {public static void main(String args[]) {ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans4.xml");ServiceExample serviceExample=(ServiceExample) ctx.getBean("serviceExample");serviceExample.adviceTest();serviceExample.adviceTest2();System.out.println("使用通知参数");ServiceExample2 serviceExample2=(ServiceExample2) ctx.getBean("serviceExample2");serviceExample2.adviceTest("hello");}}