Spring Interview Questions


Question 1:

What is the difference between Spring singleton and singleton design pattern?

The Java singleton is scoped by the Java class loader whereas the Spring singleton is scoped by the container context. Which basically means that, in Java, you can be sure a singleton only within the context of the class loader which loaded it. Other class loaders should be capable of creating another instance of it. So, Java considers a singleton in a way that it cannot create more than one instance of that class within a given class loader, whereas Spring considers a singleton in a way that it cannot create more than one instance of a class within a given container/context.

Question 2:

What are the benefits of IOC?

The main benefits of IOC are:

  • It minimizes the amount of code in your application.
  • It makes your application easy to test as it doesn't require any singletons or JNDI lookup mechanisms in your unit test cases.
  • Loose coupling is promoted with minimal effort.
  • IOC containers support eager instantiation and lazy loading of services.
Question 3:

How to configure Spring using Annotation?

By default, Annotation wiring is not turned on in the Spring container. So, before you can use annotation-based wiring, you need to enable it in your Spring configuration file. Using <context:annotation-config> you can enable annotation in your Spring application.
Example:

<?xml version="1.0" encoding="UTF-8"?>
                         <beans xmlns="http://www.springframework.org/schema/beans"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          xmlns:context="http://www.springframework.org/schema/context"
                          xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context-3.0.xsd">

                          <context:annotation-config/>
                          <!-- bean definitions go here -->

                        </beans>
Question 4:

What is Aspect Oriented Programming?

Aspect-oriented programming, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging, transaction management, data validation and authentication . In Object Oriented Programming, modularity of application is achieved by Classes whereas in AOP application modularity is achieved by Aspects and they are configured to cut across different classes methods.
AOP takes out the direct dependency of cross-cutting tasks from classes that is not possible in normal object oriented programming. For example, we can have a separate class for logging but again the classes will have to call these methods for logging the data.
The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules.

Question 5:

What are different modules in Spring?

Following are the different modules in Spring :-

  • The Core container module
  • Application context module
  • AOP module (Aspect Oriented Programming)
  • JDBC abstraction and DAO module
  • O/R mapping integration module (Object/Relational)
  • Web module
  • MVC framework module

Question 6:

What are the features/benefits provided by Spring Framework?

Following are the  features provided by Spring: 

  • Lightweight
  • Inversion of control
  • Aspect Oriented Programming
  • Container
  • MVC Framework
  • Transaction Management
  • Exception Handling
Question 7:

What is Spring IoC container?

The Spring IoC is responsible for creating the objects, managing them i.e injecting the dependency, wiring them together, configuring them, and also managing their complete lifecycle. Inversion of Control (IoC) is the mechanism to achieve loose-coupling between Objects dependencies. To achieve loose coupling and dynamic binding of the objects at runtime, the objects define their dependencies that are being injected by other assembler objects. Spring IoC container is the program that injects dependencies into an object and make it ready for our use.

                                          The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file.

Question 8:

Explain @RequestMapping annotation.

@RequestMapping annotation is used to map a URL to either an entire class or a particular method.

The @RequestMapping annotation uses the value attribute to map the method to a path. The method attribute is used to indicate the HTTP request type (example: GET, POST, DELETE).

For example:

@RequestMapping(value="/login", method=RequestMethod.GET)
public String login(){
    //Insert code here 
}
Question 9:

Explain the @Controller annotation.

The @Controller indicates the class is a Spring MVC controller stereotype which is automatically registered by context:component-scan in spring configuration file.

For Example:

@Controller
public class DemoController{
      //Insert code here
}
Question 10:

What is default scope of bean in Spring framework?

The default scope of bean is "Singleton" in Spring framework.

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media