关于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 实现机制详解** Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许程序员在不修改源代码的情况下,通过“切面”来插入额外的业务逻辑,如日志、事务管理等。AOP的引入极大地提高了代码的...
**Spring AOP 实现详解** 在Java开发中,Spring框架以其强大的功能和灵活性深受开发者喜爱。其中,AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架的一个重要特性,它允许开发者将关注点从核心业务...
2. **Spring AOP实现方式** - **代理模式**:Spring AOP使用两种代理方式,JDK动态代理和CGLIB代理。如果目标类实现了接口,Spring会使用JDK动态代理;如果没有实现接口,Spring会使用CGLIB代理生成子类。 - **JDK...
在这个"springAOP实现数据字典.zip"压缩包中,我们主要关注的是如何利用Spring AOP来实现数据字典的功能。数据字典是系统设计中一个关键的组成部分,它存储了系统中所有数据的描述,包括数据项、数据结构、数据流、...
2. **Spring AOP实现日志记载** - **配置Spring AOP**:首先,我们需要在Spring配置文件中启用AOP代理。通常,对于基于注解的配置,会使用`<aop:aspectj-autoproxy>`标签;对于XML配置,可以使用`<aop:config>`和`...
【Spring AOP实现方法大全】 在Spring框架中,面向切面编程(Aspect-Oriented Programming,简称AOP)是一种强大的设计模式,它允许我们在不修改业务代码的情况下,插入额外的功能,比如日志记录、事务管理等。在...
Spring AOP 实现原理详解之 AOP 切面的实现 Spring AOP 是基于 IOC 的 Bean 加载来实现的,本文主要介绍 Spring AOP 原理解析的切面实现过程。AOP 切面的实现是将切面类的所有切面方法根据使用的注解生成对应 ...
标题"spring aop实现日志功能"涉及到的是如何利用Spring AOP来记录和跟踪应用程序中的操作,这对于调试、性能分析和故障排查至关重要。下面我们将详细探讨如何通过Spring AOP来实现日志功能。 首先,理解AOP的基本...
在本示例中,我们将深入探讨如何利用Spring AOP实现简单的权限验证。 首先,AOP的核心概念是切面(Aspect),它封装了横切关注点,比如日志、事务管理、权限验证等。在Spring AOP中,切面通过通知(Advice)来定义...
在本文中,我们将重点探讨如何使用Spring AOP实现用户权限验证。 首先,权限验证是任何应用程序中不可或缺的一部分,尤其是涉及到用户登录和访问控制的场景。通过Spring AOP,我们可以将权限验证的逻辑与业务代码...
spring aop实现接口参数变更前后对比和日志记录完整代码,拿到项目代码,只需要做数据库连接的修改即可运行起来使用,代码案例详细,真是可靠,代码原文地址:...
【Spring AOP 实现】 Spring AOP,即Spring的面向切面编程,是一种编程范式,用于处理系统中的横切关注点,如日志、事务管理等,它将这些关注点与业务逻辑分离,提高了代码的可维护性和复用性。AOP在Spring框架中的...
Spring AOP实现详解 在Java开发中,Spring框架以其强大的功能和灵活性被广泛采用,而AOP(面向切面编程)是Spring框架的一个重要组成部分。AOP允许开发者将关注点从核心业务逻辑中分离出来,比如日志记录、权限检查...
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用
Spring AOP(面向切面编程)是Spring框架中的一个重要组件...通过理解advice、pointcut和advisor这三个核心概念,开发者可以更有效地利用Spring AOP实现日志记录、事务管理等各种功能,提高代码的可维护性和可扩展性。
在Spring AOP中,切面可以通过编写带有注解的类或者XML配置来实现。切面通常包括一个或多个通知,以及连接点的定义。 2. **通知(Advice)** 通知是在特定的连接点上执行的代码,它可以是前置通知(Before)、后置...
在这个“Spring AOP 实现”的主题中,我们将深入探讨如何通过注解在Spring中实现AOP,以及如何为AOP通知传递参数。 首先,让我们了解AOP的基本概念。AOP的核心思想是切面(Aspect),它封装了特定的横切关注点。...
动态代理则是Spring AOP实现的核心技术之一,它允许我们在运行时创建具有额外行为的对象。下面将详细阐述Spring AOP的配置以及动态代理的实现。 一、Spring AOP基础知识 1. **什么是AOP**:AOP是一种编程范式,...
本节将详细介绍如何使用Spring AOP实现流程日志跟踪,主要关注于如何通过AOP拦截特定的类和方法来进行日志记录。 ##### 3.1 配置Spring AOP 在Spring配置文件中定义切面和切入点表达式是非常关键的一步。一般来说...