CDI: Why are there unsatisfied dependencies in the following setup?

妖精的绣舞 提交于 2019-12-10 20:25:53

问题


Ear file:

ear
 +--lib
 |   +--API jar
 |   |     +-- com.foobar.Greeter.class (interface)
 |   |         (look, ma, no META-INF/beans.xml on purpose)
 |   |
 |   +--JAX-RS endpoint jar
 |         +-- com.foobar.GreeterResource.class (has @Inject Greeter greeter;)
 |         +-- META-INF/beans.xml
 |   
 +--EJB jar
 |   +-- com.foobar.GreeterBean implements com.foobar.Greeter (@Stateless, @Local)
 |   +-- META-INF/beans.xml
 |
 +--JAX-RS skinny war (no libs)
     +-- WEB-INF/beans.xml (maybe not necessary?)
     +-- WEB-INF/classes/com.foobar.Application 
                                   (empty subclass of JAX-RS Application)

To recap in English:

  • API jar file. Contains Greeter interface. Present in lib directory.
  • EJB jar file. Contains a @Stateless @Local EJB that implements Greeter. Also contains a META-INF/beans.xml file. CDI says this is necessary to make this a bean archive.
  • Skinny war. Exists only to have an (empty) Application class in it to serve as the bootstrappy part of JAX-RS. Endpoints are NOT located within. Contains a WEB-INF/beans.xml file, though I confess I'm not sure this is necessary, since by definition WEB-INF/beans.xml marks only the classes directory (which here does not exist) as a bean archive.
  • JAX-RS endpoint jar file. This one's slightly weird. Present in the .ear lib directory, so on the classpath, but not in the .war file's lib directory (on purpose). Discovered—per the JAX-RS 1.1 specification—automatically by the JAX-RS application, so classes within it are probably in a kind of tug-of-war between JAX-RS and CDI. GreeterResource contains @Inject Greeter greeter;.

Deploying this in GlassFish 3.1.2.2 (Weld 1.1.8) yields an unsatisfied dependency error for the @Inject location. WTF?

Why? The only Greeter instance in the whole application is an EJB implementation, which is in a bean archive. The resource class is in a bean archive. I read the specification as saying that session beans automatically have their business local interfaces registered as bean types.

来源:https://stackoverflow.com/questions/18008321/cdi-why-are-there-unsatisfied-dependencies-in-the-following-setup

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