Spring
framework interview questions
:
Question1:
What is IOC or inversion of control?
Ans:
This question is first step towards spring and mostly interviewer
starts from this question. As the name implies inversion of
control means now we have inverted the control of creating the
object from our own using new operator to container or framework
Now
it’s the responsibility of container to create object as required.
We
maintain one xml file where we configure our components, services,
all the classes and their property. We just need to mention which
service is needed by which component and container will create the
object for us. This concept is known as dependency injection
because all object dependency (resources) is injected into it by
framework.
Example:
In
this example CreateNewStockAccont class contain getter and setter for
newBid and container will instantiate newBid and set the value
automatically when it is used.
Ans:
Spring framework is based on IOC so we call it as IOC container also
So Beans reside inside the IOC container beans are nothing but Plain
old java object (POJO).
Following
steps explain their lifecycle inside container.
1.
Container will look the bean definition inside configuration file
(eg.bean.xml).
2
using reflection container will create the object and if any property
is defined inside the bean definition then it will also be set.
3.
If the bean implements the BeanNameAware interface, the factory calls
setBeanName() passing the bean’s ID.
4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization () methods will be called before the properties for the Bean are set.
4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization () methods will be called before the properties for the Bean are set.
6.
If an init-method is specified for the bean, it will be called.
7. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
7. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
8.
If the Bean definition in the Configuration file contains a
'destroy-method' attribute, then the corresponding method definition
in the Bean class will be called.
Question
3: what is Bean Factory, have you used XMLBean factory?
Ans:
BeanFactory is factory Pattern which is based on IoC.it is used to
make a clear separation between application configuration and
dependency from actual code.
XmlBeanFactory
is one of the implementation of bean Factory which we have used in
our project.
org.springframework.beans.factory.xml.XmlBeanFactory
is used to creat bean instance defined in our xml file.
BeanFactory
factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
Or
ClassPathResource
resorce = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resorce);
XmlBeanFactory factory = new XmlBeanFactory(resorce);
Ans:
This one is very popular spring interview question and often asks in
entry level interview. ApplicationContext is preferred way of using
spring because of functionality provided by it and interviewer wanted
to check whether you are familiar with it or not.
ApplicationContext.
|
BeanFactory
|
Here
we can have more than one config files possible
|
In
this only one config file or .xml file
|
Application
contexts can publish events to beans that are registered as
listeners
|
Doesn’t
support.
|
Support
internationalization (I18N) messages
|
It’s
not
|
Support
application lifecycle events, and validation.
|
Doesn’t
support.
|
Support
many enterprise services such JNDI access, EJB integration,
remoting
|
Doesn’t
support.
|
Question 5: What are different modules in spring?
Ans:
spring have seven core modules
1.
The
Core container module
2.
Application
context module
3.
AOP
module (Aspect Oriented Programming)
4.
JDBC
abstraction and DAO module
5.
O/R
mapping integration module (Object/Relational)
6.
Web
module