Spring
interview questions
Here I have capture important interview
questions on spring.
1. How spring
works?
1)
Spring creates an instance of bean using
reflection API.
2)
If any properties are mentioned apply them
with injection (constructor injection or setter injection)
3)
If bean implements BeanNameAware interface
then setBeanName() method will be invoked.
4)
If bean class implements
ApplicationContextAware interface then setApplicationContext() method will be
invoked.
5)
If bean implements BeanFactoryAware interface
then call setBeanFactory() method.
6)
If there are any BeanPostProcessor associated
with the BeanFactory than spring will call postProcessorBeforInitialization ()
method before the properties for the bean injected.
7)
If bean class implements the initializingBean
interface then call after propertySet() once.
8)
If there is any custom init-method declare,
call that method.
9)
If there are any BeanPostProcessor associate
with the BeanFactory that loads the bean then spring will call
postProcessorAfterInitialization() method.
Now bean is ready to use
10) If the bean class implements DisposableBean Interface than call Destroy
method.
11) If there is any custom destroy-method declared than call it.
2. What is IOC
or Dependency Injection?
The basic concept of IOC
(Dependency of Injection) is that we do not create objects but describe how they should be created.
Dependency injection is a
pattern used to create instances of objects that other objects rely on without
knowing at compile time which class will be used to provide that functionality.
Inversion of control relies on dependency injection because a mechanism is
needed in order to activate the components providing the specific functionality
Techniques to implement inversion of control are:
1)
Factory pattern
2)
Service locator pattern
3)
Dependency injection – Type of dependency
injection
a)
constructor injection
We can inject the dependency by constructor.
<bean id="BeanId"
class="com.mk.BeanClass">
<constructor-arg
value="value"
type="typeOfMember"></constructor-arg>
</bean>
b)
setter injection
We can inject the dependency by setter method also.
<bean id="BeanId"
class="com.mks.BeanClass">
<property name="PropertyName1">
<value>PropertyValue1</value>
</property>
<property name="PropertyName2">
<value>PropertyValue2</value>
</property>
</bean>
c)
interface injection
This is not implemented in spring currently, but by Avalon. It’s a
different type of DI that involves mapping items to inject to specific
interfaces.
3. What are
different spring bean scope?
1)
Singleton: Return a single bean instance per Spring IOC container.
2)
Prototype: Return a new bean instance each time when requested.
3)
Request: Return a single bean instance per HTTP request.
4)
Session: Return a single bean instance per HTTP session.
5)
Global session: Return a single bean instance per global HTTP session and only valid
when used in port let context.
4. What are
different ways to configure a class as Spring Bean?
XML Configuration: This is the most
popular configuration and we can use bean element in context file to configure
a Spring Bean.
Java Based Configuration: If you are using only annotations, you can configure a spring bean
using @Bean annotation. This annotation is used with @Configuration classes to
configure a spring bean.
Annotation Based Configuration: We can also use @Component, @Service, @Repository and @Controller
annotations with classes to configure them to be as spring bean. For these, we
would need to provide base package location to scan for these classes.
5. What are
different types of Spring Bean auto wiring?
1)
byName
2)
byType
3)
by constructor
4)
@Autowired and @Qualifier annotations
Good collection, very usefull thanks
ReplyDelete