aop 环绕通知 可以计算机 程序执行的时间
2016-06-02 17:55
441 查看
1、java代码
@Test
public void testSchemaAroundAdvice() {
System.out.println("======================================");
long start = System.currentTimeMillis();
ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");
IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);
helloworldService.sayAround("haha");
System.out.println("======================================");
long end = System.currentTimeMillis();
System.out.println("end! performance took "+(end-start)+" milliseconds");
}
2、advice.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="helloWorldService" class="cn.javass.spring.chapter6.service.impl.HelloWorldService"/>
<bean id="aspect" class="cn.javass.spring.chapter6.aop.HelloWorldAspect"/>
<bean id="beforeAdvice" class="cn.javass.spring.chapter6.aop.BeforeAdviceImpl"/>
<aop:config>
<aop:advisor pointcut="execution(* cn.javass..*.sayAdvisorBefore(..))"
advice-ref="beforeAdvice"/>
<aop:aspect ref="aspect">
<aop:before pointcut="execution(* cn.javass..*.sayBefore(..)) and args(param)"
method="beforeAdvice(java.lang.String)"
arg-names="param"/>
<aop:after-returning pointcut="execution(* cn.javass..*.sayAfterReturning(..))"
method="afterReturningAdvice"
arg-names="retVal"
returning="retVal"/>
<aop:after-throwing pointcut="execution(* cn.javass..*.sayAfterThrowing(..))"
method="afterThrowingAdvice"
arg-names="exception"
throwing="exception"/>
<aop:after pointcut="execution(* cn.javass..*.sayAfterFinally(..))"
method="afterFinallyAdvice"/>
<aop:around pointcut="execution(* cn.javass..*.sayAround(..))"
method="aroundAdvice"/>
<aop:declare-parents types-matching="cn.javass..*.IHelloWorldService+"
implement-interface="cn.javass.spring.chapter6.service.IIntroductionService"
default-impl="cn.javass.spring.chapter6.service.impl.IntroductiondService"/>
</aop:aspect>
</aop:config>
</beans>
3、切面中的 方法
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("===========around before advice");
Object retVal = pjp.proceed(new Object[] {"replace"});
System.out.println("===========around after advice");
return retVal;
}
@Test
public void testSchemaAroundAdvice() {
System.out.println("======================================");
long start = System.currentTimeMillis();
ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");
IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);
helloworldService.sayAround("haha");
System.out.println("======================================");
long end = System.currentTimeMillis();
System.out.println("end! performance took "+(end-start)+" milliseconds");
}
2、advice.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="helloWorldService" class="cn.javass.spring.chapter6.service.impl.HelloWorldService"/>
<bean id="aspect" class="cn.javass.spring.chapter6.aop.HelloWorldAspect"/>
<bean id="beforeAdvice" class="cn.javass.spring.chapter6.aop.BeforeAdviceImpl"/>
<aop:config>
<aop:advisor pointcut="execution(* cn.javass..*.sayAdvisorBefore(..))"
advice-ref="beforeAdvice"/>
<aop:aspect ref="aspect">
<aop:before pointcut="execution(* cn.javass..*.sayBefore(..)) and args(param)"
method="beforeAdvice(java.lang.String)"
arg-names="param"/>
<aop:after-returning pointcut="execution(* cn.javass..*.sayAfterReturning(..))"
method="afterReturningAdvice"
arg-names="retVal"
returning="retVal"/>
<aop:after-throwing pointcut="execution(* cn.javass..*.sayAfterThrowing(..))"
method="afterThrowingAdvice"
arg-names="exception"
throwing="exception"/>
<aop:after pointcut="execution(* cn.javass..*.sayAfterFinally(..))"
method="afterFinallyAdvice"/>
<aop:around pointcut="execution(* cn.javass..*.sayAround(..))"
method="aroundAdvice"/>
<aop:declare-parents types-matching="cn.javass..*.IHelloWorldService+"
implement-interface="cn.javass.spring.chapter6.service.IIntroductionService"
default-impl="cn.javass.spring.chapter6.service.impl.IntroductiondService"/>
</aop:aspect>
</aop:config>
</beans>
3、切面中的 方法
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("===========around before advice");
Object retVal = pjp.proceed(new Object[] {"replace"});
System.out.println("===========around after advice");
return retVal;
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- [Android]在代码里运行另一个程序的方法
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序