问题描述
我在使用 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>
解决方法
您可以尝试以下几点:
- 验证有问题的类实际上位于包含您正在编辑的
META-INF/beans.xml
并因此由您编辑的v-list-item v-for="(item,index) in profileItems" @click="item.action" ripple="ripple" :key="index"> { icon: 'mdi-exit-to-app',href: '/',title: 'Logout',action: () => { this.onUserLogout() } },]
的同一个 bean 存档中。 - 您可能希望使用
javax.annotation.Priority
annotation instead to activate your interceptor application-wide instead of bean-archive-specific。