Spring Interview QnA
Spring Interview QnA
Version Feature
Spring 2.5 This version was released
in 2007. It was the first
version which supported
annotations.
Spring 3.0 This version was released
in 2009. It made full-
fledged use of
improvements in Java5
and also provided
support to JEE6.
Spring 4.0 This version was released
in 2013. This was the first
version to provide full
support to Java 8.
Because of Spring Frameworks layered architecture, you can use what you
need and leave which you don’t.
Spring Framework enables POJO (Plain Old Java Object)
Programming which in turn enables continuous integration and testability.
JDBC is simplified due to Dependency Injection and Inversion of Control.
It is open-source and has no vendor lock-in.
5. How many modules are there in Spring Framework and what are they?
There are around 20 modules which are generalized into Core Container, Data
Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation
and Test.
Spring Core Container – This layer is basically the core of Spring
Framework. It contains the following modules :
a. Spring Core
b. Spring Bean
c. SpEL (Spring Expression Language)
d. Spring Context
Web – This layer provides support to create web application. It contains the
following modules :
a. Web
b. Web – MVC
c. Web – Socket
d. Web – Portlet
Aspect Oriented Programming (AOP) – In this layer you can use Advices,
Pointcuts etc., to decouple the code.
Instrumentation – This layer provides support to class instrumentation and
classloader implementations.
Test – This layer provides support to testing with JUnit and TestNG.
A Spring configuration file is an XML file. This file mainly contains the classes
information. It describes how those classes are configured as well as introduced
to each other. The XML configuration files, however, are verbose and more clean.
If it’s not planned and written correctly, it becomes very difficult to manage in big
projects.
Spring Framework can be used in various ways. They are listed as follows:
The container creates the object, wires them together, configures them and
manages their complete life cycle.
The container receives instructions for which objects to instantiate, configure, and
assemble by reading the configuration metadata provided. This metadata can be
provided either by XML, Java annotations or Java code.
In Dependency Injection, you do not have to create your objects but have to
describe how they should be created. You don’t connect your components and
services together in the code directly, but describe which services are needed by
which components in the configuration file. The IoC container will wire them up
together.
Constructor Injection
Setter Injection
Interface Injection
They are the objects that form the backbone of the user’s application.
Beans are managed by the Spring IoC container.
They are instantiated, configured, wired and managed by a Spring IoC
container
Beans are created with the configuration metadata that the users supply to
the container.
</bean>
<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>
@Configuration
public class StudentConfig
{
@Bean
public StudentBean myStudent()
{ return new StudentBean(); }
}
Singleton: This provides scope for the bean definition to single instance per
Spring IoC container.
Prototype: This provides scope for a single bean definition to have any
number of object instances.
Request: This provides scope for a bean definition to an HTTP-request.
Session: This provides scope for a bean definition to an HTTP-session.
Global-session: This provides scope for a bean definition to an Global HTTP-
session.
The last three are available only if the users use a web-aware ApplicationContext.
When beans are combined together within the Spring container, it’s called wiring
or bean wiring. The Spring container needs to know what beans are needed and
how the container should use dependency injection to tie the beans together,
while wiring beans.
What do you understand by auto wiring and name the different modes of it?
Instead of using XML to describe a bean wiring, the developer moves the
configuration into the component class itself by using annotations on the relevant
class, method, or field declaration. It acts as an alternative to XML setups. For
example:
@Configuration
public class AnnotationConfig
{
@Bean
public MyDemo myDemo()
{ return new MyDemoImpll(); }
}
<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">
<context:annotation-config/>
<beans ………… />
</beans>
@Component: This marks a java class as a bean. It is a generic stereotype for any
Spring-managed component. The component-scanning mechanism of spring now
can pick it up and pull it into the application context.
@Controller: This marks a class as a Spring Web MVC controller. Beans marked
with it are automatically imported into the Dependency Injection container.
The @Autowired annotation provides more accurate control over where and how
autowiring should be done. This annotation is used to autowire bean on the
setter methods, constructor, a property or methods with arbitrary names or
multiple arguments. By default, it is a type driven injection.
When you create more than one bean of the same type and want to wire only
one of them with a property you can use the @Qualifier annotation along
with @Autowired to remove the ambiguity by specifying which exact bean should
be wired.
For example, here we have two classes, Employee and EmpAccount respectively.
In EmpAccount, using @Qualifier its specified that bean with id emp1 must be
wired.
The Data Access Object (DAO) support in Spring makes it easy to work with data
access technologies like JDBC, Hibernate or JDO in a consistent way. This allows
one to switch between the persistence technologies easily. It also allows you to
code without worrying about catching exceptions that are specific to each of
these technology.
MVC (Model-View-Controller) – Spring Interview Questions
Describe DispatcherServlet.
The DispatcherServlet is the core of Spring Web MVC framework. It handles all
the HTTP requests and responses. The DispatcherServlet receives the entry of
handler mapping from the configuration file and forwards the request to the
controller. The controller then returns an object of Model And View. The
DispatcherServlet checks the entry of view resolver in the configuration file and
calls the specified view component.
Explain WebApplicationContext.
OUT OF SCOPE:
Spring AOP
Spring JDBC
Spring Boot
Spring Boot automatically configures required classes depending on the libraries on its
classpath. Suppose your application want to interact with DB, if there are Spring Data libraries
on class path then it automatically sets up connection to DB along with the Data Source class.
Spring Framework
Most important feature of Spring Framework is Dependency Injection. At the core of all Spring
Modules is Dependency Injection or IOC Inversion of Control.
When DI or IOC is used properly, we can develop loosely coupled applications. And loosely
coupled applications can be easily unit tested.
Spring MVC
Spring MVC Framework provides decoupled way of developing web applications. With simple
concepts like Dispatcher Servlet, ModelAndView and View Resolver, it makes it easy to develop
web applications.
Spring Boot
The problem with Spring and Spring MVC is the amount of configuration that is needed.
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Spring Boot solves this problem through a combination of Auto Configuration and Starter
Projects. Spring Boot also provide a few non functional features to make building production
ready applications faster.
For complete answer with code examples refer - Spring Boot vs Spring vs Spring MVC
The problem with Spring and Spring MVC is the amount of configuration that is needed.
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Can we bring more intelligence into this? When a spring mvc jar is added into an application,
can we auto configure some beans automatically?
Spring Boot looks at a) Frameworks available on the CLASSPATH b) Existing configuration for the
application. Based on these, Spring Boot provides basic configuration needed to configure the
application with these frameworks. This is called Auto Configuration.
Starters are a set of convenient dependency descriptors that you can include in your
application. You get a one-stop-shop for all the Spring and related technology that you need,
without having to hunt through sample code and copy paste loads of dependency descriptors.
For example, if you want to get started using Spring and JPA for database access, just include
the spring-boot-starter-data-jpa dependency in your project, and you are good to go.
If you want to develop a web application or an application to expose restful services, Spring
Boot Start Web is the starter to pick. Lets create a quick project with Spring Boot Starter Web
using Spring Initializr.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Following screenshot shows the different dependencies that are added in to our application
Dependencies can be classified into:
Any typical web application would use all these dependencies. Spring Boot Starter Web comes
pre packaged with these.
As a developer, I would not need to worry about either these dependencies or their compatible
versions.
What are the other Starter Project Options that Spring Boot provides?
Spring Boot also provides other starter projects including the typical dependencies to develop
specific type of applications
How does Spring enable creating production ready applications in quick time?
Spring Boot aims to enable production ready applications in quick time. Spring Boot provides a
few non functional features out of the box like caching, logging, monitoring and embedded
servers.
What is the minimum baseline Java Version for Spring Boot 2 and Spring 5?
Spring Boot 2.0 requires Java 8 or later. Java 6 and 7 are no longer supported.
Recommended Reading
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0.0-M1-Release-Notes
Spring Initializr http://start.spring.io/ is great tool to bootstrap your Spring Boot projects.
As shown in the image above, following steps have to be done
No.
Spring Initializr makes it easy to create Spring Boot Projects. But you can setup a maven project
and add the right dependencies to start off.
In Eclipse, Use File -> New Maven Project to create a new project.
Add dependencies.
Add the maven plugins!
Add the Spring Boot Application class
spring-boot-maven-plugin provides a few commands which enable you to package the code as
a jar or run the application
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
Think about what you would need to be able to deploy your application (typically) on a virtual
machine.
You would just need a virtual machine with Java installed and you would be able to directly
deploy the application on the virtual machine.
When we create an application deployable, we would embed the server (for example, tomcat)
inside the deployable.
For example, for a Spring Boot Application, you can generate an application jar which contains
Embedded Tomcat. You can run a web application as a normal Java application!
Embedded server is when our deployable unit contains the binaries for the server (example,
tomcat.jar).