Aop练手
Posted kongieg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Aop练手相关的知识,希望对你有一定的参考价值。
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <context:component-scan base-package="com.kong.bean"></context:component-scan> <bean name="myPerform" class="com.kong.service.MyPerform"></bean> <bean name="myAdvice" class="com.kong.service.MyAdvice"></bean> <aop:config> <aop:aspect ref="myAdvice"> <aop:pointcut expression="execution(* com.kong.service.MyPerform.persist(..))" id="peisistCut"/> <aop:before pointcut-ref="peisistCut" method="doBefore"/> <aop:after pointcut-ref="peisistCut" method="doAfterRegardlessIsOk"/> <aop:after-returning pointcut-ref="peisistCut" method="doAfter"/> <aop:after-throwing pointcut-ref="peisistCut" method="doAfterThrowing"/> <aop:around pointcut-ref="peisistCut" method="doSurround"/> </aop:aspect> </aop:config> </beans>
切点类:MyPerform.java
package com.kong.service; public class MyPerform public void persist() System.out.println("do persist() method!!!"); public void delete() System.out.println("do delete() method!!!"); public void modify() System.out.println("do modify() method!!!"); public void find() System.out.println("do find() method!!!");
切面:
package com.kong.service; import org.aspectj.lang.ProceedingJoinPoint; public class MyAdvice public void doBefore() System.out.println("myAdvice_method:doBefore()!!"); public void doAfter() System.out.println("myAdvice_method:doAfter()!!"); public void doAfterRegardlessIsOk() System.out.println("myAdvice_method:doAfterRegardlessIsOk()!!"); public void doAfterThrowing() System.out.println("myAdvice_method:doAfterThrowing;if program comes exception,this message will show!!!"); public void doSurround(ProceedingJoinPoint pj) throws InterruptedException try System.out.println("method doSurround() begin,hehehhe!"); System.out.println("begin drive time niddle"); Long start = System.currentTimeMillis(); Thread.sleep(2000); pj.proceed(); Long end = System.currentTimeMillis(); System.out.println("method doSurround() end,use time "+(end-start)+"mills"); catch (Throwable e) // TODO Auto-generated catch block System.out.println("Exception ocurred"); e.printStackTrace();
测试:
package com.kong.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.kong.service.MyPerform; public class MyTest @Test public void test_01() ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); MyPerform myPerform = (MyPerform) ac.getBean("myPerform"); myPerform.persist();
结果是对滴 .^_^.
以上是关于Aop练手的主要内容,如果未能解决你的问题,请参考以下文章