JSF 2.3 CDI not working on tomcat

倖福魔咒の 提交于 2019-12-11 14:48:54

问题


I am trying to set up jsf 2.3 on tomcat 8 whenever I used @inject I keep having an error with it I have googled and searched on stackoverflow.com yet I can't find a solution to it. I have already installed CDI (Weld) on it following @BalusC example from here How to install and use CDI on Tomcat? yet I keep having unsatisfied dependency: no bean matches the injection point. I can't figure it out is there anything i am missing?

ConfigurationBean.java

import static javax.faces.annotation.FacesConfig.Version.JSF_2_3;
import javax.faces.annotation.FacesConfig;
@FacesConfig(
     // Activates CDI build-in beans
     version = JSF_2_3
)
public class ConfigurationBean {
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
  version="1.1" bean-discovery-mode="all">
</beans>

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.3"
  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/web-facesconfig_2_3.xsd">
</faces-config>

PushBean.java

@Named
@ViewScoped
public class PushBean implements Serializable {
  @Inject @Push(channel="counter") //This is where i get the error message unsatisfied dependency: no bean matches the injection point
  private PushContext push;
}

For me this code looks fine but am wondering if it is netbeans bug. I tried that without using spring just only tomcat with jsf i still get the same error message. I couldn't find any error message inside the stacktrace.


回答1:


Spring is NOT a full blown CDI container and only 'knows' the @Named and @Inject annotations and consequently does not (most likely) recognize the @Push annotation as a qualifier and cannot find the bean and throws the error you get (posting an explicit error and stacktrace is btw something you should always do in a question!)

See also:

  • Inject Instance<Interface> : Spring and CDI compatibility



回答2:


I'd suggest to check your scopes. The built - in CDI scopes are @ApplicationScoped, @SessionScoped, @ConversationScoped and @RequestScoped . There is no @ViewScoped annotation in CDI. You can inject the same level or broader scope, but not one which is smaller (e.g you cannot inject @RequestScoped into @SessionScoped bean)



来源:https://stackoverflow.com/questions/50096079/jsf-2-3-cdi-not-working-on-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!