前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring3基于注释驱动的AOP

Spring3基于注释驱动的AOP

作者头像
py3study
发布2020-01-06 16:47:04
3960
发布2020-01-06 16:47:04
举报
文章被收录于专栏:python3

Spring3基于注释驱动的AOP

代码语言:javascript
复制
<!--启动spring的aop自动代理--> <aop:aspectj-autoproxy/> 

然后再创建一个AOP类

代码语言:javascript
复制
import org.aspectj.lang.JoinPoint;  import org.aspectj.lang.annotation.After;  import org.aspectj.lang.annotation.Aspect;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.stereotype.Service;   import com.pdp.biz.dao.usermanage.UserDAO;   /**   * ClassName:DemoAop   * Reason:   TODO ADD REASON   *   * @author   王涛   * @version     * @since    Ver 1.1   * @Date     2010-11-24     上午10:25:18   *   */ @Aspect @Service public class DemoAop {      @Autowired     public UserDAO userDao;            @After("execution(public * com.pdp.biz.service.usermanage.impl.UserManageServiceImpl.sayhi(..))")      public void doAfter(JoinPoint jp) {              System.out.println(userDao.findUsersCount(null));              Object[] args = jp.getArgs();              for(Object obj  : args){                  System.out.println("参数值:"+obj);              }              System.out.println("后处理切入----------"+jp.getTarget().getClass().getName()+"----"+jp.getSignature().getName());      }  } 

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

代码语言:javascript
复制
@After("execution(public * com.pdp.biz.service.usermanage.impl.UserManageServiceImpl.sayhi(..))")      public void doAfter(JoinPoint jp) { ... }

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

org.aspectj.lang Interface JoinPoint

All Known Subinterfaces: ProceedingJoinPoint


代码语言:javascript
复制
public interface JoinPoint
代码语言:javascript
复制

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.

代码语言:javascript
复制
 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了.

代码语言:javascript
复制
@Test    public void testgetUsers(){        userManageService.sayhi("Hi 大家好~!");  } 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • org.aspectj.lang Interface JoinPoint
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档