First things first i have a understanding of Difference between initializing a class and instantiating an object?
Second, the confusion arose while understanding the spring bean lifecycle.
Are the words beans initialisation and instantiation interchangeable or if bean initialization happens after bean instantiation?
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"com/springinaction/springidol/spring-idol.xml");
Performer performer = (Performer) ctx.getBean("duke");
performer.perform();
One could also use the above example when are the beans initialised and instantiated.
Initialization-Assigning a value to a variable i.e a=0,setting the initial values. Instantiation- Creating the object i.e when u r referencing a variable to an object with new operator.
Declaring - Declaring a variable means to introduce a new variable to the program. You define its type and its name. Instantiate - Instantiating a class means to create a new instance of the class.
'Instantiate Variable' essentially means that you're creating the variable and cutting the link to the original data.
The bean is instantiated and initalize by your Spring container, that's how Dependency Injection (DI) works. What you do in your code is, you declare that you want to have an instance of Performer injected in this performer variable that you have defined. Your DI context will take care to pass you this instance that is actually available in the context. You can expect a behavior like that
EDIT
If you want to influence initialization of the bean you can define an Initialization callback, see here for further information on that topic.
EDIT 2
The Spring Bean Lifecycle Tutorial gives you also a nice flow diagram what's going on in you container. If you have a look at the diagram there, after the "bean is ready to use" statement, it will get injected to your piece of code where you have declared your intended usage.
This May Come late But
I will start by mentioning three facts about spring beans life cycle that comes after loading the bean definition.
Beans are instanciated : shortly this basically means that the bean factory is calling the constructor of each bean .
Dependencies are injected : this occurs only if autowiring is done on setters or fields, we inject dependencies.
Initialization : this is the fuzzy place, shortly using @PostConstruct is the initialization phase, this will call the annotated method to initialize the state of the bean. In depth:
@PostConstruct is picked up by enabling component scanning and called by a pre-init bean named internalCommonAnnotationProcessor of a type that implements the BeanPostProcessor interface named CommonAnnotationBeanPostProcessor. Classes implementing this interface are factory hooks that allow for modifications of bean instances. The application context autodetects these types of beans and instantiates them before any other beans in the container, since after their instantiation they are used to manipulate other beans managed by the IoC container.
The BeanPostProcessor interface declares two methods to be implemented, which have been declared as default interface methods so that the developer can freely implement only the one that presents interest. The methods are named postProcessBeforeInitialization and postProcessAfterInitialization .
Typically, post processors that populate bean properties pick up methods annotated with @PostConstruct implement postProcessBeforeInitialization, while post processors that wrap beans with proxies will normally implement postProcessAfterInitialization.
source of knowledge : pivotal-certified-professional-spring-5 book on oreilly.
So My personal answer is
Constructor is about instanciation,
initialization comes always after DI is done (constructor, setter or field).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With