CDI Inject fails on maven-embedded-glassfish-plugin — org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type

后端 未结 1 1704
醉话见心
醉话见心 2021-02-20 15:07

We have a webapp, currently being developed using Java EE 7, JSF 2.2 and Glassfish 4.0. There are two particular managed beans which with a circular dependency.

UsuarioC

1条回答
  •  感动是毒
    2021-02-20 15:53

    Well, after much searching and reading we finally resolved it. Turns out, the webapp was initially developed for Java EE 6, and a decision was made to use Java EE 7 along the way. Well... somethings are different in Java EE 7. It handles managed bean scopes differently. For one thing, the @ViewScoped annotation is not even mentioned in the Java EE 7 docs anymore (there is a new @FlowScoped, but we are still reading on that). We bumped up the log level to FINEST and scoured through endless lines of details to understand what happened.

    In order to get it working with the code as it is today, we had to understand a key difference on the CDI implementation. Up until Java EE 6, CDI would scan all packages and all beans would be considered by the container. This behavior apparently changed with Java EE 7, in a way that only classes annotated with a specific scope are considered for becoming managed beans. In other words, the @Named annotation needs to be accompanied by one of the scope annotations (@RequestScoped, @SessionScoped, @DependentScoped, @FlowScoped, etc). Since @ViewScoped is no longer part of the official scope list, the class EnderecoController does not become a managed bean when CDI boots. Trying to inject an instance into UsuarioController results in a generic WELD dependency exception because an instance of that bean was never created.

    To get things working in back portability, we had to change the WEB-IBNF/beans.xml file to change attribute bean-discovery-mode="annotated" to bean-discovery-mode="all".

    
    
    
    

    Using the all discovery mode causes the CDI to include all classes for consideration while scanning for managed beans, including the ones that do not have a CDI scope annotation. We are now underway to understand the new scope management better to adapt the code to Java EE 7 standards.

    I still find it strange enough that the original code works within a complete glassfish install but not inside maven-embedded-glassfish-plugin.

    My personal rant about Java EE/CDI

    Also, I would like to explicitly comment on the absurdly broad description given by the WELD-001408 Unsatisfied dependency stacktrace. The message simply means that CDI was unable to provide a dependency injection. No details on what type of error caused the injected bean to NOT be created in the first place. Not even a "sorry, unable to find a bean to instantiate".

    An unsatisfied dependency can occur for various reasons. Any exception that occurs while trying to instantiate the dependency is hidden away from log files. You might spend an hour until you realize your bean's constructor is throwing a NullPointerException. This exception wrapping nonsense is the reason why searching for this error message on Google results in a ocean of people having the same error due to different causes.

    I hope they improve the error handling, raising exception messages so we can understand better WHY some particular dependency cannot be satisfied.

    0 讨论(0)
提交回复
热议问题