我们有一个函数注释如下
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
<bean id="AnInterface" class="com.facade.feed.AnInterfaceImpl" />
最佳答案
我使用您提供的所有信息创建了一个简单的项目,并且无法在简单的设置中重现您的问题,因此您正确地实现了 bean/方面。
一个可能的常见错误是在一个上下文中定义方面,而在另一个上下文中定义 bean,例如,方面在 applicationContext
中定义,而 bean 在 dispatcherServletContext
中定义。在这样的配置方面将不适用于子 dispatcherServletContext
中定义的 beans,仅适用于父 applicationContext
关于java - 未执行的方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047321/