A continuation from Dependency injection, delayed injection praxis. I have the Main class:
package test;
import org.springframework.beans.factory.annotation.Aut
Spring is designed for re-usable component injection, not for business data manipulation and injection.
Indeed some data are used in dependency injection, but only to configure components behavior, not to create business data holder.
By the way, the following option may be used in your case: thanks a BeanFactory
with BeanFactoryAware interface and the use of scope="prototype", you can generate a bean by invoking getBean()
like in that example or from that other question: creating bean on demand.
An alternative option if you have a limited number of beans to prepare is to use generic bean creation the same way lacking beans are mocked
Now consider that Spring never garbage collects beans in its Context. So it is risky for memory consumption to create Spring beans to hold business data.
If your aim is different (I hope so), maybe you are trying to implement by your own a multi-tenant support. Spring provides tenancy in case you have different business context to implement with specific components or behaviors.