【发布时间】:2021-04-12 13:46:55
【问题描述】:
我在使用 Weld CDI 拦截器时遇到了一些问题,我似乎无法解决。当我包含<interceptors>
ejb 项目的 beans.xml 中的标签,<class> 标签被标记为无效。 eclipse中的消息如下:
com.tura.person.service.TransactionInterceptor" 不是拦截器类 [JSR-365 §9.4]
经过一番研究,问题似乎是我将 bean-discovery-mode 设置为带注释的。我想保留该设置,那么如何在不将 bean-discovery-mode 更改为 'all' 的情况下使我的拦截器可见?
这里是拦截器接口供参考:
package com.tura.person.service;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import javax.interceptor.InterceptorBinding;
@InterceptorBinding
@Target({METHOD, TYPE})
@Retention(RUNTIME)
public @interface Transactional {}
实施:
package com.tura.person.service;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@Transactional
@Interceptor
public class TransactionInterceptor {
@AroundInvoke
public Object manageTransaction(InvocationContext ctx) throws Exception {
return null;
}
}
还有 beans.xml:
<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="annotated" >
<interceptors>
<class>com.tura.person.service.TransactionInterceptor</class>
</interceptors>
</beans>
【问题讨论】:
标签: java ejb cdi interceptor weld