Spring interview question set 1/Spring Interview Questions and Answers for Freshers & Experienced

How is the root application context in Spring MVC loaded?

The root application context is loaded using the ContextLoaderListener that belongs to the entire application. Spring MVC allows instantiating multiple DispatcherServlet and each of them have multiple contexts specific to them. They can have the same root context too.

Posted Date:- 2021-08-19 11:26:46

Can Spring MVC run on Netty?

No, Spring MVC cannot run on Netty.

Posted Date:- 2021-08-19 11:26:01

What are the common features of Mono and Flux?

The common features of Mono and Flux include the following.
1. They represent streams.
2. They can’t be executed without consuming the stream using the subscribe method.
3. They are immutable and can be called again and again to create a new instance of Flux or Mono.

Posted Date:- 2021-08-19 11:24:32

What is a proxy in Spring Framework?

A proxy in the Spring framework is referred to as the creation of an object after applying advice to a particular target object.

Posted Date:- 2021-08-19 11:22:16

What is a JoinPoint?

A JoinPoint is a point during the execution of a program and represents the method execution. It includes the execution of a method or even handling of an exception.

Posted Date:- 2021-08-19 11:21:34

What is a Pointcut in Spring?

Pointcut is an expression language of Spring AOP.

What are the different latest versions of Spring framework?

The latest versions of the Spring framework are as follows.

1. Spring 2.5
2..Spring 3.0
3. Spring 4.0

Posted Date:- 2021-08-19 11:20:20

Please explain DispatcherServlet.

The DispatcherServlet is the essence of Spring Web MVC framework and handles all the HTTP requests as well as responses. Upon receiving the entry of handler mapping from the configuration file, the DispatcherServlet forwards the request to the controller.Thereafter, the controller returns an object of Model and View. Afterward, the Dispatcher Servlet checks the configuration file for the entry of view resolver and calls the specified view component.

Posted Date:- 2021-08-19 11:18:52

Could you draw a comparison between concern and crosscutting concerns in Spring AOP?

While the concern is a behavior that the developer wants to have in a particular module of a Spring application, the cross-cutting concern is a concern that is applicable throughout the entire Spring application.

Posted Date:- 2021-08-19 11:17:42

Please explain the Dependency Injection in Spring. In how many ways can the same be used?

Instead of creating objects directly, Dependency Injection allows defining how objects should be created. As such, the code doesn’t directly contain connecting components and services together.
The configuration file has the information on which services are needed by which components. The IoC container is responsible for connecting components with the appropriate services. Dependency Injection can be used in the following forms:
1.Construction Injection
2.Setter Injection

Posted Date:- 2021-08-19 11:16:00

Name the types of transaction management that Spring supports.

Two types of transaction management are supported by Spring. They are:
a. Programmatic transaction management: In this, the transaction is managed with the help of programming. It provides you extreme flexibility, but it is very difficult to maintain.
b. Declarative transaction management: In this, the transaction management is separated from the business code. Only annotations or XML based configurations are used to manage the transactions.

Posted Date:- 2021-08-19 11:13:26

What is a Controller in Spring MVC?

Just like the MVC design pattern, the Controller is the class that takes care of all the client requests and sends them to the configured resources to handle them. In Spring MVC, DispatcherServlet is the front controller class that initializes the context based on the spring beans configurations.

Posted Date:- 2021-08-19 11:11:52

How to get ServletContext and ServletConfig object in a Spring Bean?

There are two ways to get Container specific objects in the spring bean.

Implementing Spring *Aware interfaces, for these ServletContextAware and ServletConfigAware interfaces, for a complete example of these aware interfaces, please read Spring Aware Interfaces.
Using @Autowired annotation with bean variable of type ServletContext and ServletConfig. They will work only in servlet container-specific environments only.

Posted Date:- 2021-08-19 11:11:07

What is the importance of Spring bean configuration file?

We use the Spring Bean configuration file to define all the beans that will be initialized by Spring Context. When we create the instance of Spring ApplicationContext, it reads the spring bean XML file and initializes all of them. Once the context is initialized, we can use it to get different bean instances.

Apart from Spring Bean configuration, this file also contains spring MVC interceptors, view resolvers, and other elements to support annotations-based configurations

Posted Date:- 2021-08-19 11:09:53

What is Spring IoC Container?

Inversion of Control (IoC) is the mechanism to achieve loose-coupling between Object 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 makes it ready for our use.

Spring Framework IoC container classes are part of org.springframework.beans and org.springframework.context packages and provides us different ways to decouple the object dependencies.

Posted Date:- 2021-08-19 11:08:41

What do you understand by Aspect-Oriented Programming?

Enterprise applications have some common cross-cutting concerns that are applicable to different types of Objects and application modules, such as logging, transaction management, data validation, authentication, etc. The modularity of application is achieved by classes in Object-oriented programming. In AOP, application modularity is achieved by Aspects and they are configured to cut across different class methods.

Posted Date:- 2021-08-19 11:07:25

What do you understand by @Required annotation?

Required is applied to bean property setter methods. This annotation simply indicates that the affected bean property must be populated at the configuration time with the help of an explicit property value in a bean definition or with autowiring. If the affected bean property has not been populated, the container will throw BeanInitializationException.

For example:

public class Employee
private String name;
public void setName(String name)
{this.name=name; }
public string getName()
{ return name; }

Posted Date:- 2021-08-19 11:05:37

Explain inner beans in Spring.

A bean can be declared as an inner bean only when it is used as a property of another bean. For defining a bean, the Spring’s XML based configuration metadata provides the use of <bean> element inside the <property> or <constructor-arg>. Inner beans are always anonymous and they are always scoped as prototypes. For example, let’s say we have one Student class having reference of Person class. Here we will be creating only one instance of Person class and use it inside Student.

Here’s a Student class followed by bean configuration file:

public class Student
private Person person;
//Setters and Getters
public class Person
private String name;
private String address;
//Setters and Getters


<bean id=&ldquo;StudentBean" class="com.edureka.Student">
<property name="person">
<!--This is inner bean -->
<bean class="com.edureka.Person">
<property name="name" value=&ldquo;Scott"></property>
<property name="address" value=&ldquo;Bangalore"></property>

Posted Date:- 2021-08-19 11:02:56

How many types of IOC containers are there in spring?

BeanFactory: BeanFactory is like a factory class that contains a collection of beans. It instantiates the bean whenever asked for by clients.
ApplicationContext: The ApplicationContext interface is built on top of the BeanFactory interface. It provides some extra functionality on top BeanFactory.

Posted Date:- 2021-08-19 11:00:31

In how many ways can Dependency Injection be done?

In general, dependency injection can be done in three ways, namely :
1. Constructor Injection
2. Setter Injection
3. Interface Injection
In Spring Framework, only constructor and setter injections are used.

Posted Date:- 2021-08-19 10:59:14

Is Spring 5 Compatible With Older Versions of Java?

In order to take advantage of Java 8 features, the Spring codebase has been revamped. This means older versions of Java cannot be used. So, the framework requires a minimum of Java 8.

Posted Date:- 2021-08-19 10:51:10

What Is the Use of WebClient and WebTestClient?

WebClient is a component in the new Web Reactive framework that can act as a reactive client for performing non-blocking HTTP requests. Since it's reactive client, it can handle reactive streams with back pressure, and it can take full advantage of Java 8 lambdas. It can also handle both sync and async scenarios.

On the other hand, the WebTestClient is a similar class that we can use in tests. Basically, it's a thin shell around the WebClient. It can connect to any server over an HTTP connection. It can also bind directly to WebFlux applications using mock request and response objects, without the need for an HTTP server.

Posted Date:- 2021-08-19 10:50:37

What Are the Mono and Flux Types?

The WebFlux framework in Spring Framework 5 uses Reactor as its async foundation.

This project provides two core types: Mono to represent a single async value and Flux to represent a stream of async values. They both also implement the Publisher interface defined in the Reactive Streams specification.

Mono implements Publisher and returns 0 or 1 elements:
public abstract class Mono<T> implements Publisher<T> {...}
And Flux implements Publisher and returns N elements:
public abstract class Flux<T> implements Publisher<T> {...}

By definition, the two types represent streams, and so they're both lazy. This means nothing is executed until we consume the stream using the subscribe() method. Both types are also immutable, so calling any method will return a new instance of Flux or Mono.

Posted Date:- 2021-08-19 10:49:22

What Is Weaving?

According to the official docs, weaving is a process that links aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

Posted Date:- 2021-08-19 10:46:43

What Are Aspect, Advice, Pointcut and JoinPoint in AOP?

Aspect – a class that implements cross-cutting concerns, such as transaction management
Advice – the methods that get executed when a specific JoinPoint with matching Pointcut is reached in the application
Pointcut – a set of regular expressions that are matched with JoinPoint to determine whether Advice needs to be executed or not
JoinPoint – a point during the execution of a program, such as the execution of a method or the handling of an exception

Posted Date:- 2021-08-19 10:45:45

What Is Aspect-Oriented Programming (AOP)?

Aspects enable the modularization of cross-cutting concerns such as transaction management that span multiple types and objects by adding extra behavior to already existing code without modifying affected classes.

Posted Date:- 2021-08-19 10:44:49

What Is a Controller in Spring MVC?

Simply put, all the requests processed by the DispatcherServlet are directed to classes annotated with @Controller. Each controller class maps one or more requests to methods that process and execute the requests with provided inputs.

Posted Date:- 2021-08-19 10:43:36

How Does the Scope Prototype Work?

Scope prototype means that every time we call for an instance of the Bean, Spring will create a new instance and return it. This differs from the default singleton scope, where a single object instance is instantiated once per Spring IoC container.

Posted Date:- 2021-08-19 10:42:32

What Is Spring Security?

Spring Security is a separate module of the Spring framework that focuses on providing authentication and authorization methods in Java applications. It also takes care of most of the common security vulnerabilities such as CSRF attacks.

Posted Date:- 2021-08-19 10:41:54

What are the two ways of accessing Hibernate by using Spring?

1. Inversion of Control approach by using Hibernate Template and Callback.
2.Extending HibernateDAOSupport and Applying an AOP Interceptor node.

Posted Date:- 2021-08-19 10:40:29

Can We Have Multiple Spring Configuration Files in One Project?

Yes, in large projects, having multiple Spring configurations is recommended to increase maintainability and modularity.

We can load multiple Java-based configuration files:
@Import({MainConfig.class, SchedulerConfig.class})
public class AppConfig {
Or we can load one XML file that will contain all other configs:
ApplicationContext context = new ClassPathXmlApplicationContext("spring-all.xml");

And inside this XML file we'll have the following:

<import resource="main.xml"/>
<import resource="scheduler.xml"/>

Posted Date:- 2021-08-19 10:37:40

What Is the Spring Java-Based Configuration?

It's one of the ways of configuring Spring-based applications in a type-safe manner. It's an alternative to the XML-based configuration.

Posted Date:- 2021-08-19 10:35:38

What Does the Spring Bean Life Cycle Look Like?

First, a Spring bean needs to be instantiated based on Java or XML bean definition. It may also be required to perform some initialization to get it into a usable state. After that, when the bean is no longer required, it will be removed from the IoC container.

Posted Date:- 2021-08-19 10:34:41

How to Define the Scope of a Bean?

In order to set Spring Bean's scope, we can use @Scope annotation or “scope” attribute in XML configuration files. Note that there are five supported scopes:

Posted Date:- 2021-08-19 10:31:31

What Is the Default Bean Scope in Spring Framework?

By default, a Spring Bean is initialized as a singleton.

Posted Date:- 2021-08-19 10:29:48

What Is a Spring Bean?

The Spring Beans are Java Objects that are initialized by the Spring IoC container.

Posted Date:- 2021-08-19 10:29:22

What Is the Difference Between BeanFactory and ApplicationContext?

BeanFactory is an interface representing a container that provides and manages bean instances. The default implementation instantiates beans lazily when getBean() is called.

In contrast, ApplicationContext is an interface representing a container holding all information, metadata and beans in the application. It also extends the BeanFactory interface, but the default implementation instantiates beans eagerly when the application starts. However, this behavior can be overridden for individual beans.

Posted Date:- 2021-08-19 10:28:38

Which Is the Best Way of Injecting Beans and Why?

The recommended approach is to use constructor arguments for mandatory dependencies and setters for optional ones. This is because constructor injection allows injecting values to immutable fields and makes testing easier.

Posted Date:- 2021-08-19 10:27:54

What is Spring AOP Proxy pattern?

A proxy pattern is a well-used design pattern where a proxy is an object that looks like another object but adds special functionality to it behind the scenes.
Spring AOP follows proxy-based pattern and this is created by the AOP framework to implement the aspect contracts in runtime.
The standard JDK dynamic proxies are default AOP proxies that enables any interface(s) to be proxied. Spring AOP can also use CGLIB proxies that are required to proxy classes, rather than interfaces. In case a business object does not implement an interface, then CGLIB proxies are used by default.

Posted Date:- 2021-08-19 10:27:14

What is an advice? Explain its types in spring.

An advice is the implementation of cross-cutting concerns can be applied to other modules of the spring application. Advices are of mainly 5 types:
This advice executes before a join point, but it does not have the ability to prevent execution flow from proceeding to the join point (unless it throws an exception).
To use this, use @Before annotation.
This advice is to be executed after a join point completes normally i.e if a method returns without throwing an exception.
To use this, use @AfterReturning annotation.
This advice is to be executed if a method exits by throwing an exception.

To use this, use @AfterThrowing annotation.

This advice is to be executed regardless of the means by which a join point exits (normal return or exception encounter).
To use this, use @After annotation.
This is the most powerful advice surrounds a join point such as a method invocation.
To use this, use @Around annotation.

Posted Date:- 2021-08-19 10:25:51

What is Spring AOP?

1.Spring AOP (Aspect Oriented Programming) is similar to OOPs (Object Oriented Programming) as it also provides modularity.
2. In AOP key unit is aspects or concerns which are nothing but stand-alone modules in the application. Some aspects have centralized code but other aspects may be scattered or tangled code like in the case of logging or transactions. These scattered aspects are called cross-cutting concern.
3. AOP provides platform to dynamically add these cross-cutting concerns before, after or around the actual logic by using simple pluggable configurations.
4. This results in easy maintainenance of code. Concerns can be added or removed simply by modifying configuration files and therefore without the need for recompiling complete sourcecode.
5.There are 2 types of implementing Spring AOP:
Using XML configuration files
Using AspectJ annotation style

Posted Date:- 2021-08-19 10:23:13

What Is Dependency Injection?

Dependency injection, an aspect of Inversion of Control (IoC), is a general concept stating that we do not create our objects manually but instead describe how they should be created. Then an IoC container will instantiate required classes if needed.

Posted Date:- 2021-08-19 10:19:20

What do you understand by Bean Wiring.

When beans are combined together within the Spring container, they are said to be wired or the phenomenon is called bean wiring.
The Spring container should know what beans are needed and how the beans are dependent on each other while wiring beans. This is given by means of XML / Annotations / Java code-based configuration.

Posted Date:- 2021-08-19 10:17:56

Explain Bean life cycle in Spring Bean Factory Container.

The Bean life cycle is as follows:
The IoC container instantiates the bean from the bean’s definition in the XML file.
Spring then populates all of the properties using the dependency injection as specified in the bean definition.
The bean factory container calls setBeanName() which take the bean ID and the corresponding bean has to implement BeanNameAware interface.
The factory then calls setBeanFactory() by passing an instance of itself (if BeanFactoryAware interface is implemented in the bean).
If BeanPostProcessors is associated with a bean, then the preProcessBeforeInitialization() methods are invoked.
If an init-method is specified, then it will be called.
Lastly, postProcessAfterInitialization() methods will be called if there are any BeanPostProcessors associated with the bean that needs to be run post creation.

Posted Date:- 2021-08-19 10:17:30

What are the bean scopes available in Spring?

The Spring Framework has five scope supports. They are:

Singleton: The scope of bean definition while using this would be a single instance per IoC container.
Prototype: Here, the scope for a single bean definition can be any number of object instances.
Request: The scope of the bean definition is an HTTP request.
Session: Here, the scope of the bean definition is HTTP-session.
Global-session: The scope of the bean definition here is a Global HTTP session.

Posted Date:- 2021-08-19 10:16:54

What are Spring Beans?

They are the objects forming the backbone of the user’s application and are managed by the Spring IoC container.
Spring beans are instantiated, configured, wired, and managed by IoC container.
Beans are created with the configuration metadata that the users supply to the container (by means of XML or java annotations configurations.)

Posted Date:- 2021-08-19 10:16:07

How do we implement DI in Spring Framework?

We can use Spring XML based as well as Annotation-based configuration to implement DI in spring applications. For better understanding, please read Spring Dependency Injection example where you can learn both the ways with JUnit test case. The post also contains a sample project zip file, that you can download and play around to learn more.

Posted Date:- 2021-08-19 09:47:42

What is Spring WebFlux?

Spring WebFlux is the new module introduced in Spring 5. Spring WebFlux is the first step towards the reactive programming model in the spring framework.

Posted Date:- 2021-08-19 09:47:10

What is the advantage of using Spring Framework?

Some of the advantages of using Spring Framework are:

1. Reducing direct dependencies between different components of the application. Spring IoC container is responsible for initializing resources or beans and inject them as dependencies.
2. Writing unit test cases are easy in the Spring framework because our business logic doesn’t have direct dependencies with actual resource implementation classes. We can easily write a test configuration and inject our mock beans for testing purposes.
3. Reduces the amount of boiler-plate code, such as initializing objects, open/close resources. I like JdbcTemplate class a lot because it helps us in removing all the boiler-plate code that comes with JDBC programming.
4. Spring framework is divided into several modules, it helps us in keeping our application lightweight. For example, if we don’t need 5. Spring transaction management features, we don’t need to add that dependency to our project.
Spring framework supports most of the Java EE features and even much more. It’s always on top of the new technologies, for example, there is a Spring project for Android to help us write better code for native Android applications. This makes the spring framework a complete package and we don’t need to look after the different frameworks for different requirements.

Posted Date:- 2021-08-19 09:46:30

What are some of the important features of Spring Framework?

Spring Framework is built on top of two design concepts – Dependency Injection and Aspect-Oriented Programming.

Some of the features of spring framework are:
1. Lightweight and very little overhead of using a framework for our development.
2. Dependency Injection or Inversion of Control to write components that are independent of each other, spring container takes care of wiring them together to achieve our work.
3. Spring IoC container manages Spring Bean life cycle and project-specific configurations such as JNDI lookup.
4. Spring MVC framework can be used to create web applications as well as restful web services capable of returning XML as well as JSON response.
5. Support for transaction management, JDBC operations, File uploading, Exception Handling, etc with very few configurations, either by using annotations or by spring bean configuration file.

Posted Date:- 2021-08-19 09:44:45

What is Spring Framework?

Spring is a powerful open-source, loosely coupled, lightweight, java framework meant for reducing the complexity of developing enterprise-level applications. This framework is also called the “framework of frameworks” as spring provides support to various other important frameworks like JSF, Hibernate, Structs, EJB, etc.

There are around 20 modules which are generalized into the following types:
1. Core Container
2.Data Access/Integration
4.AOP (Aspect Oriented Programming)

Posted Date:- 2021-08-19 09:40:11

R4R Team
R4R provides Spring Freshers questions and answers (Spring Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Spring interview question set 1,Spring Freshers & Experienced Interview Questions and Answers,Spring Objetive choice questions and answers,Spring Multiple choice questions and answers,Spring objective, Spring questions , Spring answers,Spring MCQs questions and answers R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for Spring fresher interview questions ,Spring Experienced interview questions,Spring fresher interview questions and answers ,Spring Experienced interview questions and answers,tricky Spring queries for interview pdf,complex Spring for practice with answers,Spring for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .