java - 未执行的方面

标签 java spring aspectj spring-aop

我们有一个函数注释如下

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);
}

Spring 配置:

<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。 让我知道是否还需要其他任何东西。任何帮助表示赞赏。

编辑:添加 Controller 以及调用 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" />

最佳答案

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

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

关于java - 未执行的方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047321/

相关文章:

unit-testing - SPRING Roo 项目 - 版本控制中应该保留什么

java - Swing:显示悬停在另一个面板上的透明面板

java - 为什么我收到 "advice has not been applied"警告?

Java Spring : Attribute 'time-unit' is not allowed to appear in element 'int:poller'

spring - 如何在 spring-servlet.xml 中映射多个数据源?

java - 在 Spring JPA Repository 的 findByTopN 中动态设置 N

java - 您可以指定一个对应于 AspectJ 中的每一行执行的切入点吗

java - Java中根据线程划分循环执行

java - 一种开发多版本java spring Rest api的方法\设计

intellij-idea - 在intellij idea中设置aspectj weaver的编译级别