Spring3基于注释驱动的AOP

简介:

Spring3基于注释驱动的AOP

实在是郁闷刚刚编辑了一篇文章,由于字数的原因,没发布成功,好我就分开写吧,今天向大家介绍的是Spring基于注释驱动的AOP,其实估计这已经不是什么新技术了,但是我争取写的通俗易懂,大家从我这看一次就能明白,那就是我最高兴的了.

还是那样,今天我主要介绍如何配置,写出一个例子,然后大家按照例子一配就ok了.配置文件很简单,只需要在Spring配置文件中加入以下这句话就行了,下面这句话是让Spring启动自动AOP代理

 

 
 
  1. <!--启动spring的aop自动代理--> 
  2. <aop:aspectj-autoproxy/> 

然后再创建一个AOP类

 

 
 
  1. import org.aspectj.lang.JoinPoint;  
  2. import org.aspectj.lang.annotation.After;  
  3. import org.aspectj.lang.annotation.Aspect;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.stereotype.Service;  
  6.  
  7. import com.pdp.biz.dao.usermanage.UserDAO;  
  8.  
  9. /**  
  10.  * ClassName:DemoAop  
  11.  * Reason:   TODO ADD REASON  
  12.  *  
  13.  * @author   王涛  
  14.  * @version    
  15.  * @since    Ver 1.1  
  16.  * @Date     2010-11-24     上午10:25:18  
  17.  *  
  18.  */ 
  19. @Aspect 
  20. @Service 
  21. public class DemoAop {  
  22.     @Autowired 
  23.     public UserDAO userDao;  
  24.       
  25.     @After("execution(public * com.pdp.biz.service.usermanage.impl.UserManageServiceImpl.sayhi(..))")  
  26.     public void doAfter(JoinPoint jp) {  
  27.             System.out.println(userDao.findUsersCount(null));  
  28.             Object[] args = jp.getArgs();  
  29.             for(Object obj  : args){  
  30.                 System.out.println("参数值:"+obj);  
  31.             }  
  32.             System.out.println("后处理切入----------"+jp.getTarget().getClass().getName()+"----"+jp.getSignature().getName());  
  33.     }  

里面的注释分别是@Aspect用于告诉Spring这个是一个需要织入的类,

 

 
 
  1. @After("execution(public * com.pdp.biz.service.usermanage.impl.UserManageServiceImpl.sayhi(..))")  
  2.     public void doAfter(JoinPoint jp) { ... }

 

里面的doAfter方法上面有一行注释,指明这个方法将在UserManageServiceImpl.sayhi(..)方法运行结束之后来执行,参数JoinPoint主要携带了参数值和方法名什么的,到时候自己查查文档就ok了

 

org.aspectj.lang 
Interface JoinPoint

All Known Subinterfaces:
ProceedingJoinPoint

public interface JoinPoint
 

 

Provides reflective access to both the state available at a join point and static information about it. This information is available from the body of advice using the special form thisJoinPoint. The primary use of this reflective information is for tracing and logging applications.

 aspect Logging {
     before(): within(com.bigboxco..*) && execution(public * *(..)) {
         System.err.println("entering: " + thisJoinPoint);
         System.err.println("  w/args: " + thisJoinPoint.getArgs());
         System.err.println("      at: " + thisJoinPoint.getSourceLocation());
     }
 }

Nested Class Summary
static interface JoinPoint.EnclosingStaticPart 
           
static interface JoinPoint.StaticPart 
          This helper object contains only the static information about a join point.
 
Field Summary
static java.lang.String ADVICE_EXECUTION 
           
static java.lang.String CONSTRUCTOR_CALL 
           
static java.lang.String CONSTRUCTOR_EXECUTION 
           
static java.lang.String EXCEPTION_HANDLER 
           
static java.lang.String FIELD_GET 
           
static java.lang.String FIELD_SET 
           
static java.lang.String INITIALIZATION 
           
static java.lang.String METHOD_CALL 
           
static java.lang.String METHOD_EXECUTION 
          The legal return values from getKind()
static java.lang.String PREINITIALIZATION 
           
static java.lang.String STATICINITIALIZATION 
           
static java.lang.String SYNCHRONIZATION_LOCK 
           
static java.lang.String SYNCHRONIZATION_UNLOCK 
           
 
Method Summary
 java.lang.Object[] getArgs() 
          Returns the arguments at this join point.
 java.lang.String getKind() 
          Returns a String representing the kind of join point.
 Signature getSignature() 
          Returns the signature at the join point.
 SourceLocation getSourceLocation() 
          Returns the source location corresponding to the join point.
 JoinPoint.StaticPart getStaticPart() 
          Returns an object that encapsulates the static parts of this join point.
 java.lang.Object getTarget() 
           Returns the target object.
 java.lang.Object getThis() 
           Returns the currently executing object.
 java.lang.String toLongString() 
          Returns an extended string representation of the join point.
 java.lang.String toShortString() 
          Returns an abbreviated string representation of the join point.
 java.lang.String toString() 
           

运行的时候直接运行就ok了.

 
 
  1. @Test    
  2. public void testgetUsers(){  
  3.       userManageService.sayhi("Hi 大家好~!");  









本文转自 tony_action 51CTO博客,原文链接:http://blog.51cto.com/tonyaction/432188,如需转载请自行联系原作者
目录
相关文章
|
29天前
|
XML 监控 安全
Spring特性之一——AOP面向切面编程
Spring特性之一——AOP面向切面编程
28 1
|
29天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
10天前
|
Java Spring
【JavaEE进阶】 Spring AOP源码简单剖析
【JavaEE进阶】 Spring AOP源码简单剖析
|
10天前
|
Java Spring
【JavaEE进阶】 Spring AOP详解
【JavaEE进阶】 Spring AOP详解
|
10天前
|
数据采集 Java 程序员
【JavaEE进阶】 Spring AOP快速上手
【JavaEE进阶】 Spring AOP快速上手
|
16天前
|
Java Spring
|
16天前
|
Java Spring
|
16天前
|
前端开发 Java Maven
Spring AOP
Spring AOP
20 1
|
16天前
|
数据采集 XML 监控
Spring AOP
Spring AOP
37 2
|
21天前
|
Java Spring 容器
Spring AOP 代码案例
Spring AOP 代码案例
34 1