Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the term bean initialisation and instantiation interchangeable

Tags:

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. enter image description here

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.

like image 345
Akash Narayan Avatar asked Dec 01 '17 09:12

Akash Narayan


People also ask

Is instantiation the same as initialization?

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.

What is the difference between declaration and instantiation an object?

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.

What does it mean to instantiate a variable?

'Instantiate Variable' essentially means that you're creating the variable and cutting the link to the original data.


2 Answers

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

  1. The DI container does a "new" on your bean class
  2. The default initialization takes place on this newly generated object aka the default constructor is called
  3. you custom init method kicks in and alters the object state like you have defined it

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.

like image 75
hecko84 Avatar answered Sep 23 '22 12:09

hecko84


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).

like image 24
Mohammed Housseyn Taleb Avatar answered Sep 20 '22 12:09

Mohammed Housseyn Taleb