CDI注射循环

问题描述:

我遇到一个问题,CDI注入到JBoss 7.1.1中的Weld容器中

I'm running into a issue with CDI Injection into a Weld container in JBoss 7.1.1

我有以下对象模型:

@Stateless
class ServiceEjb {
@Inject
A a;
}

class A {
@Inject
B b;
}

class B {
@Inject
A a;
}

当尝试在我的无状态类中注入A或B时,注入循环和崩溃使用javax.enterprise.inject.CreationException。

When trying to inject A or B in my stateless class, injection loop and crash with a javax.enterprise.inject.CreationException.

我尝试很多东西(范围界定,A或B上的@Singleton,但没有成功)。
我不想破解代码,而这些注入是有意义的。

I try many thing (scoping, @Singleton on A or B but without success). I don't want to break the code, and those injections makes senses.

任何线索都将不胜感激。

Any clues will be greatly appreciated.

CDI标准不要求循环依赖注入,除非循环中至少有一个bean具有正常范围最简单的解决方案是给A或B一个正常的范围。如果你不能给出一个正常的范围(从代码模型,它看起来都是默认的code> @Dependent 伪范围),您将不得不寻找其他解决方案。发布真实的代码示例可能会让我们为您提供特定的解决方案,但这是一个开始:

Circular dependency injection is not required by the CDI standard, unless at least one bean in the cycle has a normal scope. The easiest solution to this is to give A or B a normal scope. If you can't give either a normal scope (from the code mock-up, it looks like they all have the default @Dependent pseudo-scope), you will have to look for other solutions. Posting a real code sample might let us help you with a particular solution, but here is a start:


  • 可以将A和B组合成同一个类?

  • 可以从A和B中提取一个新类C,以便A和B @Inject C,而不是彼此?

  • Can A and B be combined into the same class?
  • Can a new class, C, be extracted from A and B, so that both A and B @Inject C instead of each other?

以下是与您可能会发现有用的其他解决方案的一些SO链接:

Here are some SO links with other solutions that you might find helpful:

具有CDI的MVP;避免循环依赖

如何避免CDI循环依赖?