Spring Framework Java Interview Questions
Categories: OTHERS
Q.1. What is Spring?
Ans. Wikipedia defines the Spring framework as “an application framework and inversion of control container for the Java platform. The framework’s core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform.” Spring is essentially a lightweight, integrated framework that can be used for developing enterprise applications in java.
Q.2. Name the different modules of the Spring framework.
Ans. Some of the important Spring Framework modules are:
(i) Spring Context – for dependency injection.
(ii) Spring AOP – for aspect oriented programming.
(iii) Spring DAO – for database operations using DAO pattern
(iv) Spring JDBC – for JDBC and DataSource support.
(v) Spring ORM – for ORM tools support such as Hibernate
(vi) Spring Web Module – for creating web applications.
(vii) Spring MVC – Model-View-Controller implementation for creating web applications, web services etc.
Q.3. List some of the important annotations in annotation-based Spring configuration.
Ans. The important annotations are:
(i) @Required
(ii) @Autowired
(iii) @Qualifier
(iv) @Resource
(v) @PostConstruct
(vi) @PreDestroy
Q.4. Explain Bean in Spring and List the different Scopes of Spring bean.
Ans. Beans are objects that form the backbone of a Spring application. They are managed by the Spring IoC container. In other words, a bean is an object that is instantiated, assembled, and managed by a Spring IoC container.
There are five Scopes defined in Spring beans.
(i) Singleton: Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure spring bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues because it’s not thread-safe.
(ii) Prototype: A new instance will be created every time the bean is requested.
(iii) Request: This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
(iv) Session: A new bean will be created for each HTTP session by the container.
(v) Global-session: This is used to create global session beans for Portlet applications.
Q.5. Explain the role of DispatcherServlet and ContextLoaderListener.
Ans. DispatcherServlet is basically the front controller in the Spring MVC application as it loads the spring bean configuration file and initializes all the beans that have been configured. If annotations are enabled, it also scans the packages to configure any bean annotated with @Component, @Controller, @Repository or @Service annotations.
ContextLoaderListener, on the other hand, is the listener to start up and shut down the WebApplicationContext in Spring root. Some of its important functions includes tying up the lifecycle of Application Context to the lifecycle of the ServletContext and automating the creation of ApplicationContext.
Q.6. What is the best way to inject dependency? Also, state the reason
Ans. The best way to inject dependencies in an application is through constructor injection. Constructor injection involves passing dependencies as parameters to a class’s constructor.
Here are the reasons why constructor injection is considered the best approach:
(i) Explicit dependencies: Constructor injection makes dependencies explicit, as they are clearly defined as constructor parameters. This improves code readability and makes it easier to understand the class’s dependencies.
(ii) Compile-time safety: With constructor injection, dependencies are resolved at compile-time rather than runtime. This allows for early detection of missing or incorrect dependencies, reducing the chances of runtime errors.
(iii) Testability: Constructor injection facilitates easy testing of classes by allowing for the injection of mock or stub dependencies during unit testing. By providing test doubles through the constructor, you can isolate the class under test and verify its behavior without relying on real dependencies.
(iv) Immutability: Constructor injection promotes immutability as dependencies can be declared as final or read-only. Immutable objects are generally easier to reason about and less prone to bugs related to state changes.
(v) Dependency inversion principle: Constructor injection adheres to the Dependency Inversion Principle (DIP) of the SOLID principles. DIP states that high-level modules should not depend on low-level modules, but both should depend on abstractions. Constructor injection allows for the injection of abstractions rather than concrete implementations, enabling flexibility and loose coupling between classes.
While other dependency injection techniques like setter injection and field injection exist, they have certain drawbacks compared to constructor injection. Setter injection can lead to optional dependencies or unexpected null values, while field injection tightly couples classes and makes it harder to identify dependencies. Hence, constructor injection is generally recommended as the best way to inject dependencies.