In this article, I'll discuss some of the most important Spring interview questions.
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.
2. What are the benefits of IOC?
The main benefits of IOC are:
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.
<?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>
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.
5. What are different modules in Spring?
Following are the different modules in Spring:
6. What is Spring IoC container?
The Spring IoC is responsible for creating the objects, managing them i.e injecting 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.
7. What is default scope of bean in Spring framework?
The default scope of bean is "Singleton" in Spring framework.
8. Can you instantiate a static inner class in Spring?
If for whatever reason you want to configure a bean definition for a static inner class, you have to use the binary name of the inner class.
For example, if you have a class called Foo in the
com.example package, and this Foo class has a static inner class calledBar, the value of the 'class' attribute on a bean definition would be...
Notice the use of the $ character in the name to separate the inner class name from the outer class name.
9. What is an advice?
Advice is the action taken by an action at a particular join point. In other words, this is an actual piece of code that is invoked when a certain join point with matching pointcut is reached in the application.
For example Implementation for logging/Log4j.
10. What are the types of the transaction management Spring supports?
Spring Framework supports:
In this article, we discussed some of the most frequently asked Spring interview questions.
To read more Spring interview question visit Spring Interview Question.
Attempt Spring Multiple Choice Question at Spring MCQ