关于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 输出方法执行时间
分享到:
相关推荐
NULL 博文链接:https://zhang-yingjie-qq-com.iteye.com/blog/319927
Spring AOP的实现机制中文版,动态代理及原理,自定义类加载器
java springAOP实现数据字典
博客《理解Spring AOP实现与思想》案例代码,对Java技术感兴趣的朋友可以关注一下我,我在csdn为您准备了很多Java干货。
Spring AOP实现,项目源码,Myeclipse直接导入可用,本人根据教程编写,测试通过。
该资源包含了一个springAOP实现的简单实例,简单易懂,喜欢的下载。
本篇文章主要介绍了Java之Spring AOP 实现用户权限验证,用户登录、权限管理这些是必不可少的业务逻辑,具有一定的参考价值,有兴趣的可以了解一下。
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用
spring aop实现日志功能 开发技术-其它
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现.doc
Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现.doc
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
用spring aop 编写的权限验证实例代码,下载后将解压后的文件直接导入到eclipse中,直接运行测试方法即可。
该压缩包中包含了一个myeclipse6.5下开发的JAVA基于spring实现的日志记载例子,该例子提供了接受切点参数,解析切点返回值,并且都打印出来了,有详细的文档介绍。
springaop多数据库读写分离
用SpringAop思想写共享单车按小时计费,用代理类来实现计费
演示了使用spring aop拦截方法进行数据验证,并结合注解实现