當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring学习(22)--- AOP之Advice应用(下)
生活随笔
收集整理的這篇文章主要介紹了
Spring学习(22)--- AOP之Advice应用(下)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(六)Advice parameters(advice帶參數的情況)
例子:
修改MyAspect(添加around_init方法):
package com.aop.schema;import org.aspectj.lang.ProceedingJoinPoint;/** * * 切面類 * */ public class MyAspect {public void before(){System.out.println("MyAspect.before");}public void afterreturning(){System.out.println("MyAspect.afterreturning");}public void afterthrowing(){System.out.println("MyAspect.afterthrowing");}public void after(){System.out.println("MyAspect.after");}public void around(ProceedingJoinPoint pjp) {try {System.out.println("MyAspect.around_1");Object obj=pjp.proceed();System.out.println("MyAspect.around_2");} catch (Throwable e) {e.printStackTrace();}}public void around_init(ProceedingJoinPoint pjp,String name,int age) {System.out.println(name+" "+age);try {System.out.println("MyAspect.around_1");Object obj=pjp.proceed();System.out.println("MyAspect.around_2");} catch (Throwable e) {e.printStackTrace();}} }修改ApsectBiz類(添加init方法):
package com.aop.schema; /** * * 業務類 * */ public class ApsectBiz {public void biz(){System.out.println("ApsectBiz.biz");//throw new RuntimeException(); //故意拋出異常}public void init(String name,int age){System.out.println("ApsectBiz.init : "+ name +" " +age);} }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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><bean id="myAspect" class="com.aop.schema.MyAspect"></bean><bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean><aop:config><aop:aspect id="myAspectAOP" ref="myAspect"><!-- 先注釋掉,便于觀察結果<aop:pointcut id="myPointcut" expression="execution(* com.aop.schema.ApsectBiz.*(..))" /><aop:before method="before" pointcut-ref="myPointcut"/><aop:after-returning method="afterreturning" pointcut-ref="myPointcut"/><aop:after-throwing method="afterthrowing" pointcut-ref="myPointcut"/><aop:after method="after" pointcut-ref="myPointcut"/><aop:around method="around" pointcut-ref="myPointcut"/>--><aop:around method="around_init" pointcut="execution(* com.aop.schema.ApsectBiz.init(String,int)) and args(name,age)"/></aop:aspect></aop:config></beans>單元測試:
package com.aop.schema;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class UnitTest {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");ApsectBiz biz = (ApsectBiz)context.getBean("apsectBiz");biz.init("Json",25);} }結果:
七月 09, 2015 11:48:42 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@118e0f0f: startup date [Thu Jul 09 23:48:42 CST 2015]; root of context hierarchy 七月 09, 2015 11:48:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring-aop.xml] Json 25 MyAspect.around_1 ApsectBiz.init : Json 25 MyAspect.around_2?
轉載于:https://www.cnblogs.com/JsonShare/p/4634535.html
總結
以上是生活随笔為你收集整理的Spring学习(22)--- AOP之Advice应用(下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 报错:未能加载文件或程序集“XXX”或它
- 下一篇: DM8168 新三板系统启动