Spring Interview Questions

What is Spring ?

It is a lightweight, loosely coupled and integrated framework for developing enterprise applications in java.

What are the advantages of spring framework?

Loose Coupling

Easy to test

Lightweight

Fast Development

Powerful Abstraction

Declarative support

Predefined Templates

What is Dependency Injection?

The process of creating an object for some other class and let the class directly using the dependency is called DI.

These are the design patterns that are used to remove dependency from the programming code. 
They make the code easier to test and maintain. Let's understand this with the following code:

class Employee{  
Address address;  
Employee(){  
address=new Address();  
}  
}  

In such case, there is dependency between the Employee and Address (tight coupling). 
In the Inversion of Control scenario, we do this something like this:

class Employee{  
Address address;  
Employee(Address address){  
this.address=address;  
}  
}  
Thus, IOC makes the code loosely coupled. In such case, there is no need to modify the code if our logic is moved to new environment.

In Spring framework, IOC container is responsible to inject the dependency. We provide metadata to the IOC container either by XML file or annotation.

What is IOC and DI?

IOC (Inversion of Control) and DI (Dependency Injection) is a design pattern to provide loose coupling. 
It removes the dependency from the program.


What is the role of IOC container in spring?

IOC container is responsible to:

create the instance
configure the instance, and
assemble the dependencies

What are the types of IOC container in spring?

There are two types of IOC containers in spring framework.

BeanFactory
ApplicationContext

Bean Factory container - This is the simplest container providing basic support for DI .
The BeanFactory is usually preferred where the resources are limited like mobile devices or applet based applications

Spring ApplicationContext Container - This container adds more enterprise-specific functionality such as the ability to 
resolve textual messages from a properties file and the ability to publish application events to interested event 
listeners.


What are the different types of IOC (dependency injection) ?

There are three types of dependency injection:

* Constructor Injection : Dependencies are provided
as constructor parameters.

* Setter Injection : Dependencies are assigned through JavaBeans
properties (ex: setter methods).

* Interface Injection : Injection is done through an interface.

Note: Spring supports only Constructor and Setter Injection


 Differentiate between singleton and prototype bean?

Singleton means only one bean is defined per object instance 
while Prototype means one definition to more than one object instances in Spring. Singleton is default scope.


What is the difference between constructor injection and setter injection?


No.Constructor InjectionSetter Injection
1)No Partial InjectionPartial Injection
2)Desn't override the setter propertyOverrides the constructor property if both are defined.
3)Creates new instance if any modification occursDoesn't create new instance if you change the property value
4)Better for too many propertiesBetter for few properties.