【问题标题】:Aspects not getting executed未执行的方面
【发布时间】:2014-11-20 18:52:47
【问题描述】:

我们在一个函数上有如下注解

public class AnInterfaceImpl implements AnInterface {
    @FairThreadUsageByEntity(entityName = "XYXYXYX",
    numberOfThreads = 1)
    public Report getReport(final String One, final String Two) {
        //implementation.
    }
}

public interface AnInterface {
    String BEAN_NAME = "AnInterface";   //used for injection in spring.
    Report getReport(final String One, final String two);
}

弹簧配置:

<aop:aspectj-autoproxy />
<bean class="com.amazon.utils.fairthreadusage.aspect.FairThreadUsageByEntityAdvice" />

注解作为方面实现。基本功能是限制特定类型功能使用的线程数,比如下载。下面是注解FairThreadUsageByEntity的代码:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface FairThreadUsageByEntity {
    public String entityName();
    public int numberOfThreads();
}

@Aspect
public class FairThreadUsageByEntityAdvice extends FairThreadUsageBase {

    @Around("@annotation(fairUsage)")
    public Object fairThreadUsageByEntity(final ProceedingJoinPoint pjp, final FairThreadUsageByEntity fairUsage)
            throws Throwable {  
        //Implementation
    }
}

注释无法以某种方式工作。我正在使用 AspectJWeaver 1.7 和 java 1.7。 让我知道是否需要其他任何东西。任何帮助表示赞赏。

编辑:添加控制器以及调用 getReport 函数

public class ReportDownloadRootController extends BaseRootController {
    public static final String REQUEST_MAPPING_REPORT_DOWNLOAD = "/hz/inventory/addproducts/status/report";
    public static final String PARAM_REFERENCE_ID = "reference_id";
    private AnInterface anInterface;

    @RequestMapping(REQUEST_MAPPING_REPORT_DOWNLOAD)
    public void execute(@RequestParam(value = PARAM_REFERENCE_ID, required = true) final String referenceId,
        final HttpServletRequest request, final HttpServletResponse response) {
        try {

            Report report = AnInterface.getReport(referenceId, getContext().getMerchantId());    //breakpoint here
        } catch {
            //something
        }
    }
    @Resource(name = AnInterface.BEAN_NAME)
    public void setAnInterface(final AnInterface anInterface) {
        this.anInterface = anInterface;
    }
}

编辑 2:AnInterface 的 Spring bean

<bean id="AnInterface" class="com.facade.feed.AnInterfaceImpl" />

【问题讨论】:

  • 可能是因为您使用的是普通 JDK 代理,并且它们无权访问代理对象。如果您将&lt;aop:aspectj-autoproxy /&gt; 更改为&lt;aop:aspectj-autoproxy proxy-target-class="true"/&gt; 以使用CGLIB,会发生什么情况?
  • @MikeKobit 也没有多少人尝试过该选项。参考:stackoverflow.com/questions/9310927/…
  • 由于您使用的是 Spring AOP,我不确定您使用 AspectJWeaver-1.7 是什么意思。 &lt;aop:aspectj-autoproxy /&gt; 允许您为 AOP 代理使用 @Aspect 样式注释。您能否通过设置断点来验证您的调用是否是 a) 通过代理进行的以及 b) 您的建议是否正在被检查?
  • @MikeKobit 请查看编辑。我认为它澄清了更多的事情。我在调用获取报告的位置设置了一个断点。名为Clib2AOPProxy 的类在调用getReport 之前拦截,但未检查建议。
  • AnInterfaceImpl.getReport(..) 上的注解应该是@FairThreadUsageByEntity,而不是@AnAnnotation。这只是一个复制和粘贴问题,还是你真的在那里使用了错误的注释?

标签: java spring aspectj spring-aop


【解决方案1】:

我使用您提供的所有信息创建了一个简单的项目,并且无法在简单的设置中重现您的问题,因此您正确实现了您的 bean/方面。

一个可能的常见错误是在一个上下文中定义方面而在另一个上下文中定义 bean,例如,方面在 applicationContext 中定义,而 bean 在 dispatcherServletContext 中定义。在这样的配置方面将不适用于子 dispatcherServletContext 中定义的 bean,仅适用于父 applicationContext

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多