beans.xml Interceptor Invalid with Discovery Mode 'annotated'

问题描述

我在使用 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>

解决方法

您可以尝试以下几点:

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...