Pranay Munshi
Pranay Munshi

Reputation: 71

Spring Java Config reference in bean constructor to get other bean in constructed class

I've seen one of the program where MyConfig(Spring Configuration file) is being referenced in constructor of one of the bean in order to get other bean defined in MyConfig. I am not sure about this kind of configuration. I can see cyclic reference in this kind of code, though it is working fine but I am not able to understand the flow. How it is working. Below is the replica of that code -

@Configuration
public class MyConfig {

   @Bean(name="a")
   @Scope("prototype")
   public A getA() {
      return new A();
   }

   @Bean(name="b")
   @Scope("prototype")
   public B getB() {
      return new B();
   }

   @Bean(name="c")
   @Scope("prototype")
   public C getC() {
      return new C();
   }

   @Bean(name="queueListener")
   @Scope("singleton")
   public Queue getQueue() {
      return new Queue(MyConfig config);
   }

}

Here is my Queue class -

public class Queue implements MessageListener{

    private MyConfig config;

    public Q(MyConfig config) {
       this.config = config;
    }

    @Override
    public void onMessage() {
       createC();
    }

    public void createC() {
       C cObj = config.getC();
       cObj.setConfig(config);
       cObj.performTask();
    }
}

The class is "C" look like this-

public class C{

   private transient MyConfig config;
   private MyConfig config;

   public C() {    
   }

   public void setConfig(MyConfig config) {
      this.config = config;
   }

   public MyConfig getConfig() {
      return config;
   }

   public void performTask() {     
      A a = config.getA();  // Is it right way to get bean?
      B b = config.getB(); 
   }

} 

So my question is that is it right way to get bean in another bean? Will the return object really be spring bean object or simple java class object?

I can see cyclic reference in above code cause When instance of Queue class will be created inside MyConfig will take instance/reference of MyConfig. Will this create cyclic reference? My Architect has suggested me above approach instead of autoWiring Application context in both classes Queue and class "C". According to architect context is very heavy and it is not best practice.

What will be the execution cycle or call hierarchy when bean Queue is getting created? It would be very much helpful to understand the working of above code.

Upvotes: 0

Views: 973

Answers (2)

Egor Lyashenko
Egor Lyashenko

Reputation: 797

It is a really bad idea to inject the configuration instance into a specific bean. It complicates your code and makes it inconvenient for testing because for testing Queue instance you should somehow mock the whole configuration.

If you want to inject a prototype bean to the singleton you can use a technique described here : Howto generate prototype objects with in a singleton bean using spring java configurations

Upvotes: 1

Antoni Alatalo
Antoni Alatalo

Reputation: 3

Please see example at Spring Boot @Autowired creating instances on a runtime You can see how to use singleton and prototypes right way

Upvotes: 0

Related Questions