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