关于Spring的Aop实现,总结如下
必须的几个包:
asm-2.2.2.jar
asm-commons-2.2.2.jar
asm-util-2.2.2.jar
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
当然,还有:
spring.jar
commons-logging.jar
其他,按需选用
jtds-1.2.2.jar
log4j-1.2.15.jar
proxool-0.9.0RC3.jar
其中的两种实现方式
目标执行类:
package com.shenzhoufu.pay.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**
* User: libofeng
* Date: 2008-3-9
* Time: 18:51:35
* Description:todo
*/
public class TestManager extends JdbcDaoSupport {
public void testTime() {
getJdbcTemplate().execute("select top 1 * from xxxxx");
//throw new RuntimeException("i am run into error!!!");
}
}
一、注解方式
package com.shenzhoufu.pay.aop.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
/**
* User: libofeng
* Date: 2008-3-10
* Time: 11:59:35
* Description:注解切面
*/
@Aspect
public class AnnotationAspect {
@Pointcut(value = "execution(* com.shenzhoufu.pay.dao.TestManager.*
(..))")
private void annotationPointcut() {
}
@Around(value = "annotationPointcut()")
public Object testRun(ProceedingJoinPoint pjp) throws Throwable {
StopWatch clock = new StopWatch(getClass().getName());
System.out.println("start Around");
clock.start(pjp.toShortString());
Object retVal = pjp.proceed();
System.out.println("haha, i am working!!!");
clock.stop();
System.out.println(clock.prettyPrint());
System.out.println("stop Around");
return retVal;
}
}
配置文件:
<!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="testManager" class="com.shenzhoufu.pay.dao.TestManager"/>
<bean id="annotationAspect"
class="com.shenzhoufu.pay.aop.aspect.AnnotationAspect"/>
执行结果:
2008-03-10 13:32:48,111 INFO [test.com.shenzhoufu.pay.dao.TestManagerTest]
136 - <Loading context for:
classpath*:/resources/cn/com/shenzhoufu/pay/spring/*.xml>
2008-03-10 13:32:49,101 DEBUG [com.shenzhoufu.pay.web.ErrorsController]
298 - <Found action method [public
org.springframework.web.servlet.ModelAndView
com.shenzhoufu.pay.web.ErrorsController.handleHttp404
(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletRespo
nse) throws javax.servlet.ServletException]>
2008-03-10 13:32:49,104 DEBUG [com.shenzhoufu.pay.web.ErrorsController]
298 - <Found action method [public
org.springframework.web.servlet.ModelAndView
com.shenzhoufu.pay.web.ErrorsController.defaultHandle
(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletRespo
nse) throws javax.servlet.ServletException]>
2008-03-10 13:32:49,298 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final org.springframework.jdbc.core.JdbcTemplate
org.springframework.jdbc.core.support.JdbcDaoSupport.getJdbcTemplate()]
because it is final: All calls to this method via a proxy will be routed
directly to the proxy.>
2008-03-10 13:32:49,301 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final void
org.springframework.jdbc.core.support.JdbcDaoSupport.setDataSource
(javax.sql.DataSource)] because it is final: All calls to this method via
a proxy will be routed directly to the proxy.>
2008-03-10 13:32:49,303 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final javax.sql.DataSource
org.springframework.jdbc.core.support.JdbcDaoSupport.getDataSource()]
because it is final: All calls to this method via a proxy will be routed
directly to the proxy.>
2008-03-10 13:32:49,305 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final void
org.springframework.jdbc.core.support.JdbcDaoSupport.setJdbcTemplate
(org.springframework.jdbc.core.JdbcTemplate)] because it is final: All
calls to this method via a proxy will be routed directly to the proxy.>
2008-03-10 13:32:49,308 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final void
org.springframework.dao.support.DaoSupport.afterPropertiesSet() throws
java.lang.IllegalArgumentException,org.springframework.beans.factory.BeanI
nitializationException] because it is final: All calls to this method via
a proxy will be routed directly to the proxy.>
start Around
2008-03-10 13:32:49,441 DEBUG [org.springframework.jdbc.core.JdbcTemplate]
364 - <Executing SQL statement [select top 1 * from xxxxxx]>
haha, i am working!!!
StopWatch 'com.shenzhoufu.pay.aop.aspect.AnnotationAspect': running time
(millis) = 507
-----------------------------------------
ms % Task name
-----------------------------------------
00507 100% execution(testTime)
stop Around
2008-03-10 13:32:49,952 INFO [org.logicalcobwebs.proxool.hibernatea-mssql]
490 - <Shutting down 'hibernatea-mssql' pool immediately [Shutdown Hook]>
2008-03-10 13:32:49,958 INFO
[org.logicalcobwebs.proxool.PrototyperController] 100 - <Stopping
Prototyper thread>
2008-03-10 13:32:49,959 INFO
[org.logicalcobwebs.proxool.HouseKeeperController] 107 - <Stopping
HouseKeeper thread>
二、配置文件方式
切面类:
package com.shenzhoufu.pay.aop.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* User: libofeng
* Date: 2008-3-10
* Time: 11:33:41
* Description:简单切面类
*/
public class TestAspect {
public Object testAspect(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
System.out.println("start stopwatch");
Object retVal = pjp.proceed();
System.out.println("haha, i am working!!!");
// stop stopwatch
System.out.println("stop stopwatch");
return retVal;
}
}
执行结果:
2008-03-10 13:32:03,840 INFO [test.com.shenzhoufu.pay.dao.TestManagerTest]
136 - <Loading context for:
classpath*:/resources/cn/com/shenzhoufu/pay/spring/*.xml>
2008-03-10 13:32:04,912 DEBUG [com.shenzhoufu.pay.web.ErrorsController]
298 - <Found action method [public
org.springframework.web.servlet.ModelAndView
com.shenzhoufu.pay.web.ErrorsController.handleHttp404
(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletRespo
nse) throws javax.servlet.ServletException]>
2008-03-10 13:32:04,915 DEBUG [com.shenzhoufu.pay.web.ErrorsController]
298 - <Found action method [public
org.springframework.web.servlet.ModelAndView
com.shenzhoufu.pay.web.ErrorsController.defaultHandle
(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletRespo
nse) throws javax.servlet.ServletException]>
2008-03-10 13:32:05,131 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final org.springframework.jdbc.core.JdbcTemplate
org.springframework.jdbc.core.support.JdbcDaoSupport.getJdbcTemplate()]
because it is final: All calls to this method via a proxy will be routed
directly to the proxy.>
2008-03-10 13:32:05,134 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final void
org.springframework.jdbc.core.support.JdbcDaoSupport.setDataSource
(javax.sql.DataSource)] because it is final: All calls to this method via
a proxy will be routed directly to the proxy.>
2008-03-10 13:32:05,137 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final javax.sql.DataSource
org.springframework.jdbc.core.support.JdbcDaoSupport.getDataSource()]
because it is final: All calls to this method via a proxy will be routed
directly to the proxy.>
2008-03-10 13:32:05,139 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final void
org.springframework.jdbc.core.support.JdbcDaoSupport.setJdbcTemplate
(org.springframework.jdbc.core.JdbcTemplate)] because it is final: All
calls to this method via a proxy will be routed directly to the proxy.>
2008-03-10 13:32:05,141 WARN
[org.springframework.aop.framework.Cglib2AopProxy] 250 - <Unable to proxy
method [public final void
org.springframework.dao.support.DaoSupport.afterPropertiesSet() throws
java.lang.IllegalArgumentException,org.springframework.beans.factory.BeanI
nitializationException] because it is final: All calls to this method via
a proxy will be routed directly to the proxy.>
start stopwatch
2008-03-10 13:32:05,296 DEBUG [org.springframework.jdbc.core.JdbcTemplate]
364 - <Executing SQL statement [select top 1 * from xxxxxx]>
haha, i am working!!!
stop stopwatch
2008-03-10 13:32:05,831 INFO [org.logicalcobwebs.proxool.hibernatea-mssql]
490 - <Shutting down 'hibernatea-mssql' pool immediately [Shutdown Hook]>
2008-03-10 13:32:05,835 INFO
[org.logicalcobwebs.proxool.PrototyperController] 100 - <Stopping
Prototyper thread>
2008-03-10 13:32:05,836 INFO
[org.logicalcobwebs.proxool.HouseKeeperController] 107 - <Stopping
HouseKeeper thread>
请特别注意执行目标方法时异常的处理方式,参照 spring Aop 输出方法执行时间