Monday, March 23, 2015

Solving Circular Dependencies in CDI

If you are using CDI in your project and if you are using it in such a way that there are circular dependencies:
@Singleton @AC
class A {
   @Inject @DC D d;


@Singleton @DC
class D {
    @Inject @AC A a;


You are likely to end up with issues like:
org.jboss.weld.exceptions.DeploymentException: WELD-001443: Pseudo scoped bean has circular dependencies. Dependency path:

You can go ahead and use the javax.inject.Provider to resolve these issues in such a way.
@Singleton @AC
class A {
   @Inject @DC D d;


@Singleton @DC
class D {
    @Provider Provider<A> a;


Then you may get the instance of Class A within any method of D as follows:
A aInstance = a.get();

This way you can solve circular dependency issues of such nature.

No comments: