Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Application Context available inside the constructor

I am having an issue when trying to create beans from a spring Application Context inside a bean instantiated by spring using constructor arguments.

I have implemented the ApplicationContextAware interface but it populates the context after the instance is created (obvious).

But then, if you need to get beans from the constructor, and I am talking about a variable number of objects defined at runtime, what would be the correct way to proceed?

like image 619
pedromarce Avatar asked Oct 16 '09 08:10

pedromarce


1 Answers

In beans instanciated by Spring, instead of initializing it in the Constructor, initialize it in a dedicated method, marked as "init-method" for Spring.

You have about the same effect as initializing in the constructor, but you are using the correct Spring life-cycle.

like image 53
KLE Avatar answered Nov 02 '22 23:11

KLE