Tôi muốn viết bộ chặn đánh chặn CDI. Việc chặn hoạt động tốt nếu chú thích của tôi chỉ chứa 1 tham số nhưng ngắt nếu 2 tham số được sử dụng. Câu hỏi đặt ra là tại sao?Trình chặn đánh chặn Java EE CDI không hoạt động khi chú thích có thông số
Lớp đánh chặn:
@Monitored
@Interceptor
@Priority(APPLICATION)
public class MonitoringInterceptor {
@AroundInvoke
public Object logInvocation(InvocationContext ctx) throws Exception {
LOGGER.error("METHOD CALLED!!!"); //this is not called when annotation has 2 parameters
return ctx.proceed();
}
}
Chú thích:
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Monitored {
public String layer() default "BUSINESS";
public String useCase() default "N/A";
}
Bây giờ là phần thú vị:
@Stateless
public class MyBean {
//this does not work, why?
@Monitored(layer = "BUSINESS", useCase = "test")
//if I use the following annotation it works well
//@Monitored(layer = "BUSINESS")
public String sayHello(String message) {
return message;
}
}
Tôi biết rằng MyBean
không chú thích với @Interceptors
. Đây là dự định. Thiết bị chặn được tuyên bố trong beans.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
<interceptors>
<class>my.package.MonitoringInterceptor</class>
</interceptors>
</beans>
Cảm ơn bạn, sau khi thêm '@ Nonbinding' vào các tham số đã làm việc. – flash