Home / Interview / Spring :: General Questions

Interview :: Spring

1) What is Spring?

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

2) What are the advantages of spring framework?
  1. Predefined Templates
  2. Loose Coupling
  3. Easy to test
  4. Lightweight
  5. Fast Development
  6. Powerful Abstraction
  7. Declarative support
3) What are the modules of spring framework?
  1. Test
  2. Spring Core Container
  3. AOP, Aspects and Instrumentation
  4. Data Access/Integration
  5. Web
4) 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.

Let's write a code without following IOC and DI.

Now, there is dependency between Employee and Address because Employee is forced to use the same address instance.

Let's write the IOC or DI code.

Now, there is no dependency between Employee and Address because Employee is not forced to use the same address instance. It can use any address instance.

5) What is the role of IOC container in spring?

IOC container is responsible to:

  • create the instance
  • configure the instance, and
  • assemble the dependencies
6) What are the types of IOC container in spring?

There are two types of IOC containers in spring framework.

  1. BeanFactory
  2. ApplicationContext
7) What is the difference between BeanFactory and ApplicationContext?

BeanFactory is the basic container whereas ApplicationContext is the advanced container. ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory such as integration with spring AOP, message resource handling for i18n etc.

8) 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.
9) What is autowiring in spring? What are the autowiring modes?

Autowiring enables the programmer to inject the bean automatically. We don't need to write explicit injection logic.

Let's see the code to inject bean using dependency injection.

The autowiring modes are given below:

No.ModeDescription
1)nothis is the default mode, it means autowiring is not enabled.
2)byNameinjects the bean based on the property name. It uses setter method.
3)byTypeinjects the bean based on the property type. It uses setter method.
4)constructorIt injects the bean using constructor

The "autodetect" mode is deprecated since spring 3.

10) What are the different bean scopes in spring?

There are 5 bean scopes in spring framework.

No.ScopeDescription
1)singletonThe bean instance will be only once and same instance will be returned by the IOC container. It is the default scope.
2)prototypeThe bean instance will be created each time when requested.
3)requestThe bean instance will be created per HTTP request.
4)sessionThe bean instance will be created per HTTP session.
5)globalsessionThe bean instance will be created per HTTP global session. It can be used in portlet context only.