For every object or an entity that has existence will have lifecycle.
Lifecycle = is nothing but the actions or activities that are taken part on top of the object during its existence is called lifecycle.
For every object to have its lifecycle, there are few mandatory activities to be performed on top of the object after the object has been born or before the object is going to be destroyed, such mandatory activities that have to be conducted on top of the object once it is born and before it begins lifecycle or before it is going to die are called " lifecycle management activities ".
For every object, there are 2 mandatory lifecycle management activities are there
1. birth lifecycle
2. death lifecycle
class Calculator {
int I;
int j;
public Calculator() {
i = 10;
j = 20;
}
int add() {
sum = i + j;
return sum;
}
}
Calculator cal = new Calculator();
cal.init();
1. we are requesting the JVM to create the object
2. jvm is creating the object
we want to perform post-construction logic after the object has been born
// no we need to perform initialization or post-construction activity on top of the object.
cal.init();
cal.add();
Calculator cal1 = new Calculator();
cal1.init();
cal1.add();
Bean Lifecycle
After ioc container has created the object for the bean definition, after ioc container has injected all the dependencies into the bean definition object, then using all the values that are supplied during creating, we want to perform post construction logic, similar before ioc container is removing the bean definition object from the ioc container we want to perform pre-destruction logic ontop of the bean definition, this can be done through bean lifecycle
since the ioc container is taking care of instantiating, and managing the dependencies, its the responsibility of the ioc container itself to provide an mechanism of performing post construction and pre-destruction activities for the bean definition
There are 3 ways of working with bean lifecycle are there:
1. Configuration Approach
2. Programmatic Approach
3. Annotation-driven Approach
1 Configuration Approach
In case of configuration approach we define the lifecycle management methods for the bean definition to the ioc container using spring bean configuration file, so it is called " configuration approach ".
class Calculator {
int i;
int j;
int sum;
public Calculator(int i) {
this.i = i;
}
public void setJ(int j) {
this.j = j;
}
public void init() {
this.sum = this.i + this.j;
}
public void destroy() {
sop("cleaning up...");
}
}
application-context.xml
-----------------------
<bean id="calculator" class="Calculator" init-method="init" destroy-method="destroy">
<constructor-arg value="10"/>
<property name="j" value="20"/>
</bean>
Test.java
---------
BeanFactory beanFactory= new XmlBeanFactory(new ClassPathResource("com/bl/common/application-context.xml"));
Calculator calculator = beanFactory.getBean("calculator", Calculator.class);
sop(calculator);
when does the ioc container will call the init-method="init" we configured?
after creating the object of the bean definition, after performing constructor injection, setter injection and aware injection before placing the object within the ioc container, it invokes the init-method lifecycle management method on the bean definition.
destroy-method? :
The ioc container will not invoke the destroy method, why?
The destroy method on the bean definition object is supposed to be called by the ioc container at the time of removing the object from the ioc container.
class A {
public A() {}
public void finalize() {}
}
A a = new A();
a = null; // immediately the object becomes qualified for garbage collection
The finalize() method on an object will be invoked by the Garbage Collector at the time reclaiming the memory of the object.
When does the object will be garbage collected?
whenever the object became dangling pointer, means it is not referenced by any other objects within the jvm then the object will be qualified for garbage collection
The finalize() method on every object is not guaranteed to be called by the GC, why?
When does the garbage collector will be executed?
The Garbage collector is an daemon thread that is executed only at periodically interval of time and we (programmer) dont have control over executing the gc, only we can request for execution by calling System.gc();
How does an object knows why it is going to be garbage collected or destroyed or removed from the jvm memory?
when the gc invokes finalize() method on the object, the object will know it has to perform cleanup activities since it getting destroyed or removed. Every object may or may not know when it is going to die, since the finalize() method is not guaranteed to be called on the object.
Comments
Post a Comment