首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring AOP介绍不支持&& this(IntroducedInterface)

Spring AOP介绍不支持&& this(IntroducedInterface)
EN

Stack Overflow用户
提问于 2017-08-28 13:45:32
回答 2查看 221关注 0票数 0

我有以下几个方面

代码语言:javascript
运行
AI代码解释
复制
@Aspect
public class AspectClass{
   @DeclareParents(value="com.mac.model.*",defaultImpl=Impl.class)
   public IntroduceInterface inter;

   @Pointcut("execution(* com.mac.Employee.display(..))")
   public void empPointcut(){}

  @Before("empPointCut() && this(introduceInterface)")
  public void BeforeAdvice(JoinPoint jp,IntroduceInterface inf){
  inf.introMethod();
  }
}

我正在尝试从spring文档中复制代码,如下所示:

代码语言:javascript
运行
AI代码解释
复制
@Aspect
public class UsageTracking {

    @DeclareParents(value="com.xzy.myapp.service.*+", defaultImpl=DefaultUsageTracked.class)
    public static UsageTracked mixin;

    @Before("com.xyz.myapp.SystemArchitecture.businessService() && this(usageTracked)")
    public void recordUsage(UsageTracked usageTracked) {
        usageTracked.incrementUseCount();
    }

}

但是它不工作它给出的错误: IllegalArgumentException错误在::0在切入点中正式取消绑定

这是一个简单的spring应用程序,.What可能是它不工作的原因吗?

EN

回答 2

Stack Overflow用户

发布于 2017-08-28 16:39:36

在这里输入姓名

代码语言:javascript
运行
AI代码解释
复制
@Before("empPointCut() && this(_name_in_here_)")

应与中相同

代码语言:javascript
运行
AI代码解释
复制
public void BeforeAdvice(JoinPoint jp,IntroduceInterface _name_in_here_){

所以这应该可以很好地工作:

代码语言:javascript
运行
AI代码解释
复制
@Aspect
public class AspectClass{
   @DeclareParents(value="com.mac.model.*",defaultImpl=Impl.class)
   public IntroduceInterface inter;

   @Pointcut("execution(* com.mac.Employee.display(..))")
   public void empPointcut(){}

  @Before("empPointCut() && this(inf)")
  public void BeforeAdvice(JoinPoint jp,IntroduceInterface inf){
  inf.introMethod();
  }
}
票数 0
EN

Stack Overflow用户

发布于 2017-08-29 04:54:27

Annotation对象可通过annotation参数提供给通知。因此,这两个名称(方法参数名称和在切入点表达式中声明的注释)应该相同。

请看一下spring documentation - Passing parameters to advice中的这两部分。

来自Spring文档:

代理对象( this)、目标对象( target)和注释( @within、@target、@annotation、@args)都可以以类似的方式进行绑定。下面的示例展示了如何匹配使用@Auditable注解注释的方法的执行,并提取审计代码。

首先是@Auditable注释的定义:

代码语言:javascript
运行
AI代码解释
复制
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auditable {
    AuditCode value();
}

然后是与@Auditable方法的执行相匹配的建议:

代码语言:javascript
运行
AI代码解释
复制
@Before("com.xyz.lib.Pointcuts.anyPublicMethod() && @annotation(**auditable**)")
public void audit(Auditable **auditable**) {
    AuditCode code = auditable.value();
    // ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45920565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档