For CDI injection, do I need META-INF/beans.xml in the producer archive AND the injectee archive?

StackOverflow https://stackoverflow.com/questions/10915956

  •  13-06-2021
  •  | 
  •  

문제

Perhaps it's just too many hours of combing through the CDI 1.0 specification, but I couldn't find a detailed description of exactly where a META-INF/beans.xml was required.

(The section on bean discovery makes a few tantalizing statements that imply certain things, but I'm not sure if it's prescriptive.)

I understand that loosely speaking any bean archive needs one.

But consider a case with the following jars in a compliant CDI 1.0 environment:

  • An interface jar containing interfaces only
  • An implementation jar containing CDI beans that implement those interfaces and that feature @Inject injection points, but no producer methods
  • A producer jar containing (for simplicity) a single Java object with a single method that has been annotated with @Produces.

It seems logical that both the producer jar and the implementation jar would need to be bean archives, and so hence would each need META-INF/beans.xml resources contained within them.

But for completeness, is this in fact the case? Since the implementation jar only uses an @Inject point and no CDI annotations, would it be scanned by other means? Another way to look at it: in this example, the implementation jar is used only as the target for injections, not the source of injectables. So I could see some sort of logic that would say: look, only jars that produce beans that can be injected need to be declared as bean archives. (I'm not saying that's true; just that I could see that argument being made.)

Finally, there's the whole issue of the interface jar. By definition it doesn't contain beans, but I've seen cases long ago with GlassFish where even the interface jar had to have a META-INF/beans.xml file in it. That seems wrong to me, but I found to my surprise that I couldn't really find a section in the specification to confirm or deny this.

So: beans.xml everywhere? Only in injectable-producing jars? Only in injection-target jars? Or...?

도움이 되었습니까?

해결책

a.) You need the beans.xml marker file in both producer and impl jar

b.) You don't need the beans.xml marker file on pure interface jars. If GlassFish fails, then this is a gf bug. It doesn't do any harm though.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top