gpt4 book ai didi

jakarta-ee - 拦截器真的默认禁用了吗?

转载 作者:行者123 更新时间:2023-12-03 09:45:12 26 4
gpt4 key购买 nike

我今天遇到了这种困惑。 Quote from Weld's documentation (right under Section 9.3) ,

By default, all interceptors are disabled. We need to enable our interceptor. We can do it using beans.xml descriptor of a bean archive. However, this activation only applies to the beans in that archive.



但是,在我目前正在进行的项目中,我有一个用于分析方法的拦截器。我的 META-INF/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"
version="1.1" bean-discovery-mode="all">
</beans>

然而,我仍然从该分析拦截器中获得预期的日志。那么,正如标题所说,拦截器真的默认禁用了吗?

顺便说一句,我使用 weld-se对于项目中的 CDI 功能,因为 CDI 是 Java EE 堆栈中项目唯一需要的东西。

更新

今天捣乱了拦截器后,我发现如果旧的 @Interceptors用于表示拦截实现类, beans.xml 中无需指定任何内容.但是,如果您使用拦截器绑定(bind),即使用 @Interceptor注解指示拦截器类,您必须通过将拦截器类添加到 beans.xml 来启用拦截.根据我的经验,CDI 1.1 仍然如此,如 beans.xml 中的版本所示。多于。顺便说一句,我使用 org.jboss.weld.se:weld-se:2.0.4.Final对于这种情况下的 CDI 实现,我相信它实现了 CDI 1.1。

最佳答案

在他的编辑中确认 JBT 的发现。根据 JEE6 规范的 Weld 1.0 实现的 JSR-299 的 CDI 规范 1.0。请引用 this pointer ,我引用:

By default, a bean archive has no enabled interceptors bound via interceptor bindings. An interceptor must be explicitly enabled by listing the fully qualified class name in a child <class> element of <interceptors>. as in Approach 2 below



跟随一个例子:

第一个强制性步骤是拦截器绑定(bind):
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Loggable {
}

第二个强制性步骤是拦截类:
@Interceptor
@Loggable
public class LoggingInterceptor {

@AroundInvoke
public Object logMethodEntry(InvocationContext ctx) throws Exception{
System.out.println("In LoggingInterceptor..................... before method call");
Object returnMe = ctx.proceed();
System.out.println("In LoggingInterceptor..................... after method call");
return returnMe;
}
}

第三步可以使用以下任一方法实现

方法 1,一个空 beans.xml 将完成这项工作
@Stateless
@Interceptors(LoggingInterceptor.class) //class interception
public class Displayer implements DisplayerLocal {

@Override
//@Interceptors(LoggingInterceptor.class) //method interception
public void displayHi() {
System.out.println(".....Hi there............");
}
}

方法2,需要beans.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>com.companyname.LoggingInterceptor</class>
</interceptors>
</beans>

然后是截取的类:
@Stateless
@Loggable //class interception
public class Displayer implements DisplayerLocal {

@Override
//@Loggable //method interception
public void displayHi() {
System.out.println(".....Hi there............");
}
}

关于jakarta-ee - 拦截器真的默认禁用了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com