Внедрение CDI в фильтр сервлетов Tomcat 7.0.50

Я хотел бы внедрить bean-компонент в фильтр сервлета, как описано в разделе https://stackoverflow.com/a/7815328/802058 но у меня не работает. Моя конфигурация:

@FacesConfig(
    version = Version.JSF
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]
...
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]
...

Что случилось?


person Toru    schedule 26.02.2018    source источник


Ответы (1)


CDI/Weld не может найти соответствующий bean-компонент из-за квалификатора @FacesConfig (об этом сообщает трассировка трассировки). Добавьте квалификатор @Default в FooBean. Квалификатор @Default — это квалификатор, который есть у компонента, если он явно не указывает квалификатор.

В качестве альтернативы вы можете добавить квалификатор @Any в свою точку внедрения. Как следует из названия, любой bean-компонент по умолчанию имеет квалификатор @Any.

person Alex Fire    schedule 27.02.2018
comment
Хорошо, если я удалю тег Faces Config, это сработает. Но мне непонятно, почему этот тег смущает дикое поведение. Мне нужен этот тег, потому что в противном случае некоторые функции 2.3 вообще не работают (см. github.com/javaee/ Glassfish/issues/22094). - person Toru; 28.02.2018