【问题标题】:CDI Injection in Servlet Filter Tomcat 7.0.50Servlet 过滤器 Tomcat 7.0.50 中的 CDI 注入
【发布时间】:2018-08-06 00:24:51
【问题描述】:

我想按照https://stackoverflow.com/a/7815328/802058 中的说明将 bean 注入 servlet 过滤器,但它对我不起作用。我的配置是:

@FacesConfig(
    version = Version.JSF_2_3
)
@Named
@SessionScoped
public class FooBean implements Serializable {

@WebFilter("/foo.xhtml")
public class FooFilter implements Filter {
    @Inject
    private FooBean fooBean;

WEB-INF
    lib
        javax.faces-2.3.0.jar
        omnifaces-1.14.1.jar
        weld-servlet-shaded-3.0.2.Final.jar
beans.xml (empty)

Tomcat 启动后的错误信息:

INFORMATION: Starting Servlet Engine: Apache Tomcat/7.0.50
Feb 26, 2018 2:41:37 PM org.jboss.weld.environment.servlet.EnhancedListener onStartup
INFO: WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
Feb 26, 2018 2:41:37 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 3.0.2 (Final)
Feb 26, 2018 2:41:38 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Feb 26, 2018 2:41:38 PM org.jboss.weld.event.ExtensionObserverMethodImpl checkRequiredTypeAnnotations
INFO: WELD-000411: Observer method [BackedAnnotatedMethod] public org.omnifaces.VetoAnnotatedTypeExtension.processAnnotatedType(@Observes ProcessAnnotatedType<T>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
Feb 26, 2018 2:41:38 PM org.jboss.weld.bootstrap.events.BeforeBeanDiscoveryImpl addAnnotatedType
WARN: WELD-000146: BeforeBeanDiscovery.addAnnotatedType(AnnotatedType<?>) used for class com.sun.faces.flow.FlowDiscoveryCDIHelper is deprecated from CDI 1.1!
Feb 26, 2018 2:41:39 PM org.jboss.weld.environment.tomcat.TomcatContainer initialize
INFO: WELD-ENV-001100: Tomcat 7+ detected, CDI injection will be available in Servlets, Filters and Listeners.
Feb 26, 2018 2:41:40 PM org.apache.catalina.core.ContainerBase startInternal
SCHWERWIEGEND: A child container failed during start
...
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type FooBean with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private filter.FooFilter.fooBean
  at filter.FooFilter.fooBean(FooFilter.java:0)
WELD-001475: The following beans match by type, but none have matching qualifiers:
  - Managed Bean [class beans.fooBean] with qualifiers [@FacesConfig @Any @Named]
...

怎么了?

【问题讨论】:

    标签: jsf tomcat7 cdi servlet-filters weld


    【解决方案1】:

    CDI/Weld 找不到匹配的 bean,因为 @FacesConfig 限定符(这是 stracktrace 告诉你的)。将 @Default 限定符添加到 FooBean。 @Default 限定符是 bean 没有明确指定限定符时所具有的限定符。

    或者,您可以将@Any 限定符添加到您的注入点。顾名思义,任何 bean 默认都有 @Any 限定符。

    【讨论】:

    • 好的,如果我删除 Faces Config 标记就可以了。但我不清楚为什么这个标签会混淆狂野的行为。我需要这个标签,因为否则某些 2.3 功能根本不起作用(请参阅github.com/javaee/glassfish/issues/22094)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2013-01-23
    • 2016-11-09
    • 2012-07-29
    • 2014-08-30
    • 2018-12-13
    • 2014-03-22
    相关资源
    最近更新 更多