Frequently asked Spring Interview Questions with best answers for freshers and experienced developers on Spring AOP, JDBC, MVC, Context etc.
Spring is an open source framework which reduces the complexity of enterprise application development. Spring makes Java application simple, improves testability and provide loose coupling between components.
Spring provides loose coupling through Inversion of control (IoC), also known popularly among developers as Dependency Injection (DI). When we use IoC, container given the dependencies passively to an object rather than an object looking for its dependencies from a container. During instantiation, container gives all the dependencies associated with an object without waiting for the object to make a formal request.
4. Explain the advantages of Spring?
- With the help of Spring we can make our application code less dependent on framework.
- It promotes pluggability: With Spring you can think of application objects as named services. Thus you can swap one service for another without affecting the rest of the application.
- Provides good programming practices: Using an IoC container like Spring reduces the complexity of coding to interfaces, rather than classes.
- Spring applications are easy to test: Application objects will generally be POJOs and POJOs are easy to test.
- Reusing existing components: Spring does not introduce its own solution in areas such as ORM, logging, connection pool or other system services. But, spring makes sure that these components are significantly easier to use.
5. What is Aspect Oriented Programming (AOP)?
Spring provides AOP which enables cohesive application development due to the clear demarcation of business logic from other crosscutting concern, such as auditing, logging and transaction management. Due to this clear separation, application objects perform only business logic and not concerned about the system concerns such as logging or transaction support.
6. Explain the difference between concern and crosscutting concern in Spring AOP?
The concern can be defined as a functionality/feature a programmer wants to implement to address a specific business problem. For example, In eCommerce applications a concern can be stock management, shipping management etc.
Crosscutting concern is a concern than span across almost all modules of an application. For example, logging, security etc.
7. What is Aspect?
An aspect can be defined as modularization of a crosscutting concern. For example, a logging module would be called AOP aspect for logging.
8. What is Joinpoint?
The Joinpoint is defined as the point in your application where we inject aspects. Usually when we write a method for executing a business logic, aspects are plugged in at following Joinpoints.
- Before the start of business logic method
- Once business logic method end
- When business logic throws exception
9. What is Advice?
It is the actual action taken up by an aspect at a particular Joinpoint before or after the method execution. It is the actual piece of code that get executed by the Spring AOP framework during the execution of a program.
10. What are the different types of advices?
Spring AOP support following type of advices
- Before advice : Run an advice before a method executes.
- After advice: Run advice after a method executes.
- After returning advice: Run advice on successful execution of a method.
- After throwing advice: Run advice if a method throws an exception.
- Around advice: Run advice before the methods and it can make a decision on whether the original method can be called or not
11. What is Pointcut?
The Pointcut is an expression language of Spring which determines whether an Advice should run at any Joinpoint based on the criteria specified in the Pointcut expression.
12. What is Introduction?
The Spring introduction allows us to add new methods or attributes to any advised object.