0

I have a hierarchy of beans dependency of same parent class S as:

A -> B -> C

where Bean A contains bean B, bean B contains C with code structure something like this:

public class A extends S {
    private S successor;
}

public class B extends S {
    private S successor;       
}

public class C extends S {       
    private S successor;
}

And when implementing I have

A a = new A();
B b = new B();
C c = new C();
a.successor = b;
b.successor = c;

What I really want to do here to set all the immediate above bean creation and dependency relationships in the Configuration instead of hardcoding in the code; something like:

@Configuration
public class MyConfig {


    @Bean
    public A a {
        return new A();
    }

    @Bean
    public B b {
        B b = new B();
        A a; // somehow get the bean A here
        a.successor = b;
    }

    @Bean
    public C c {
        C c = new C();
        B b; // somehow get the bean b here
        b.successor = c;
    }
}

Any inputs on how to go about this using Spring boot Dependency injection?

neaGaze
  • 1,381
  • 22
  • 28

1 Answers1

1

You can pass required beans as arguments to your provider methods.

@Bean
public A a() {
    return new A();
}

@Bean
public B b(A a) {
    B b = new B();
    a.successor = b;
    return b;
}

@Bean
public C c(B b) {
    C c = new C();
    b.successor = c;
    return c;
}

However, this sets the a.successor only when B is injected somewhere. I believe this is not what you expect, and inverting the construction chain is desired:

@Bean
public A a(B b) {
    A a = new A();
    a.successor = b;
    return a;
}

@Bean
public B b(C c) {
    B b = new B();
    b.successor = c;
    return b;
}

@Bean
public C c() {
    return new C();
}
Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • That is one way but my question here is how will the bean A and B know which instance of bean B and C is called respectively. We may have multiple instances of beans. In that case, A or B don't which bean we are using. Is there something that can be written around @Autowired that will help resolve the correct bean successor? – neaGaze Feb 11 '18 at 04:06
  • You didn’t mention that multiple instances are needed in the original question. Are qualifiers enough to solve your problem? See for example https://stackoverflow.com/questions/40830548/spring-autowired-and-qualifier – Lesiak Feb 11 '18 at 08:46
  • OK That helped. I tried your way and its almost working except for the one last impediment. The beans are not injected inside the class that I wanted to use them in. They are however injected in the `service` classes. I define that class as `Component`, still it didn't work. Have you faced this issue before? – neaGaze Feb 11 '18 at 15:01
  • update: I managed to solve that last issue as well. I was creating an object of the `Component` class where I was consuming all the beans using a traditional java new syntax instead of injecting the bean for it. Thank you so much @Lesiak :D – neaGaze Feb 11 '18 at 15:18