5

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.

Akash Narayan
  • 300
  • 3
  • 14

2 Answers2

4

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.

hecko84
  • 1,224
  • 1
  • 16
  • 29
  • My understanding about bean initialisation is that this happens when `getBean()` is called, but this also returns the instance of the Performer. Does this mean here (in Spring) the bean initialisation and bean instantiation happens at the same time... i am definitely missing something ! – Akash Narayan Dec 01 '17 at 09:52
  • If your code just look like that you will receive an already initialized bean. Nevertheless, I just researched a little bit and you have the option to define an initialization callback, meaning you can influence how the initialization takes place. I will add this to my answer – hecko84 Dec 01 '17 at 09:58
  • That's very right. We can manipulate bean initialisation using `init-method=methodName` in the context xml' ; but the same section also mentions - "The `init-method` attribute specifies a method that is to be called on the bean immediately upon instantiation." And then in the sample code displays - public class ExampleBean { public void init() { // do some initialization work } } – Akash Narayan Dec 01 '17 at 10:14
  • Examples like these makes me ask these questions ;) – Akash Narayan Dec 01 '17 at 10:16
  • So, in your Performer class you can have a void no argument method where you can put your custom initialization and you can configure your xml to set that as init-method. The Spring DI container will call this init method right after it instantiates it in the DI context and before it gets passed to the piece of code where you inject it. – hecko84 Dec 01 '17 at 10:24
  • Do you mean- The bean object first is initialised with its default dependencies, and then gets instantiated, and then before being injected into the desired class once again gets initialised(/overridden) with new values inside the `int-methods` method. – Akash Narayan Dec 01 '17 at 10:34
  • 1
    1. Bean gets instantiated - The DI container does a "new" on your bean class 2. the default initialization takes place on this newly generated object 3. you custom init metho kicks in and alters the object state like you define it – hecko84 Dec 01 '17 at 10:46
  • If you could also add these details in the answer it will make it perfect. – Akash Narayan Dec 01 '17 at 11:02
1

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