代码之家  ›  专栏  ›  技术社区  ›  Toru

Servlet过滤器Tomcat 7.0.50中的CDI注入

  •  0
  • Toru  · 技术社区  · 6 年前

    我想将bean注入servlet过滤器,如下所述 https://stackoverflow.com/a/7815328/802058 但这对我不起作用。我的配置是:

    @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]
    ...
    

    发生了什么?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Alex Fire    6 年前

    CDI/Weld找不到匹配的bean,因为@FacesConfig限定符(stracktrace就是这么告诉您的)。向FooBean添加@默认限定符。@Default限定符是bean在没有显式指定限定符的情况下所具有的限定符。

    或者,您可以向注入点添加@任何限定符。顾名思义,默认情况下,任何bean都有@any限定符。