【问题标题】:beans.xml Interceptor Invalid with Discovery Mode 'annotated'beans.xml 拦截器无效,发现模式“注释”
【发布时间】: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


    【解决方案1】:

    您可以尝试一些事情:

    【讨论】:

    • 根据 1.2 规范 (docs.jboss.org/cdi/spec/1.2/…) @Interceptor 是一个定义注解的 bean,除非我弄错了。即使我将架构更新为 beans_2_0(与项目的 cdi 版本匹配),问题仍然存在。
    • 是的,你说的很对。我将编辑我的答案以删除第一个要点。
    猜你喜欢
    • 2018-09-26
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2012-01-12
    相关资源
    最近更新 更多