Interview Questions
Question:
- Explain about bean life cycle in spring framework?
Answer:
- Spring container is very much capable to hide most
of the architecture complexity and about the complex communication that happens
between the spring container and the spring beans. Below are the number of
activities mentioned that take place between the time of bean instantiation and
during the handover of the bean reference to the client application.
Spring Bean Life Cycle Steps:-
Instantiate: - spring container instantiates the bean.
Poluplate
properties: - spring container injects the bean properties.
SetBeanName: - If the bean implements BeanNameAware interface, spring passes
the bean’s ID to setBeanName() factory method.
SetBeanFactory: - If the bean implements the BeanFactoryAware interface, spring passes the BeanFactory instance to setBeanFactory() method.
postProcessBeforeInitialization:-
If there are any BeanPostProcessors associated with
the bean, spring calls their postProcessBeaforeInitialization() method.
Initialize
Beans: - If the bean implements InitializingBean, its
afterPropertiesSet() method will be called. If the bean has a custom init
method declared, the specified Initialization method will be called.
postProcessAfterInitialization:- If there are any BeanPostProcessors associated with the bean, spring
calls their portProcessAfterInitialization() method.
Bean
is ready to use: - At this point the bean is ready to be used by
the application and will remain.
Destroy
bean: - If the bean implements DisposableBean, its destroy
() method will be called, if the bean has a custom destroy-method decalred then
the specified method will be called.
Question: - What is InitializingBean and DisposableBean
interfaces in spring and how to use them?
Answer: - The
InitializingBean and DisposableBean are two marker interfaces available in
spring framework which calls the afterPropertiesSet () in the begining and
destroy () method is invoked as part of the last action for destroying or destructing the bean.
Example
to use Spring InitializingBean and DisposableBean
interfaces are given below:-
System Requirements:-
- Eclipse Editor or any other.
- JDK 1.5 or higher(I am using jdk 1.7.0_03)
- Spring jars.
Required Jars are:-
- spring-core.jar
- spring-2.5.jar
- spring-beans.jar
- spring-context.jar
- commons-logging.jar
- dom4j-1.4.jar
- antlr.jar
- log4j.jar
Steps for creating
Eclipse java project for implementing spring InitializingBean
and DisposableBean interfaces:-
- Create a java project in eclipse.
- Create a package in the src folder with the name as com.gaurav.spring.core.initdestroy.impl.
- Create the below files in this package and place the corresponding code in those files.
- Create an XML file and name it as spring-core-initializablebean-disposablebean-impl.xml and place this file in the classpath outside the src folder.
- Execute the DatabaseServiceCaller.java by selecting the option Run as Java Application.
DatabaseServices.java
package
com.gaurav.spring.core.initdestroy.impl;
import
org.springframework.beans.factory.DisposableBean;
import
org.springframework.beans.factory.InitializingBean;
public class
DatabaseServices implements InitializingBean, DisposableBean {
private
String userName;
private
String password;
private
String url;
private
String driverClassName;
/**
* @return the userName
*/
public
String getUserName() {
return
userName;
}
/**
* @param userName
*
the userName to set
*/
public
void setUserName(String userName) {
this.userName
= userName;
}
/**
* @return the password
*/
public
String getPassword() {
return
password;
}
/**
* @param password
*
the password to set
*/
public
void setPassword(String password) {
this.password
= password;
}
/**
* @return the url
*/
public
String getUrl() {
return
url;
}
/**
* @param url
*
the url to set
*/
public
void setUrl(String url) {
this.url
= url;
}
/**
* @return the driverClassName
*/
public
String getDriverClassName() {
return
driverClassName;
}
/**
* @param driverClassName
*
the driverClassName to set
*/
public
void setDriverClassName(String driverClassName) {
this.driverClassName
= driverClassName;
}
@Override
public
void afterPropertiesSet() throws Exception {
System.out
.println("afterPropertiesSet()
method is called and the required properties values for establishing database
connection are : \nDriverClassName: "
+
driverClassName
+
" \nURL: "
+
url
+
" \nUserName: "
+
userName + "\nPassword: " + password);
}
@Override
public
void destroy() throws Exception {
System.out
.println("destroy()
method is invoked and all database properties are destroyed");
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
id="databaseServiceID"
class="com.gaurav.spring.core.initdestroy.impl.DatabaseServices">
<property
name="userName" value="system" />
<property
name="password" value="system" />
<property
name="url" value="jdbc:oracle:thin:@localhost:1521:XE"
/>
<property
name="driverClassName" value="oracle.jdbc.driver.OracleDriver"
/>
</bean>
</beans>
DatabaseServiceCaller.java
package
com.gaurav.spring.core.initdestroy.impl;
import
org.springframework.context.ConfigurableApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class
DatabaseServiceCaller {
public
static void main(String[] args) {
ConfigurableApplicationContext
context = new ClassPathXmlApplicationContext(
new
String[] { "spring-core-initializablebean-disposablebean-impl.xml"
});
DatabaseServices
databaseServicesBean = (DatabaseServices) context
.getBean("databaseServiceID");
System.out.println(databaseServicesBean);
context.close();
}
}
Result:-
log4j:WARN No appenders could be
found for logger
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the
log4j system properly.
afterPropertiesSet() method is
called and the required properties values for establishing database connection
are :
DriverClassName:
oracle.jdbc.driver.OracleDriver
URL:
jdbc:oracle:thin:@localhost:1521:XE
UserName: system
Password: system
com.gaurav.spring.core.initdestroy.impl.DatabaseServices@1304ef4
destroy()
method is invoked and all database properties are destroyed
Note: - The
afterPropertiesSet() method is called, after all the DatabaseServices properties
is set with the values, while the destroy() method is called after the
context.close().
No comments:
Post a Comment