Interview Questions.

Spring Interview Questions

fluid

Spring Interview Questions

Ever on account that its launch lower back in October of 2002, Spring Framework has succeeded in turning into a pinnacle utility framework for Java developers. In reality, it secures an area among the main backend net improvement frameworks. Hence, choosing a profession in Spring Framework could be very profitable.

Top Spring Interview Questions and Answers

Spring Framework has a wide array of standards that one desires to have a working expertise on the way to live beforehand in the sport. If you’re making ready for a Spring activity interview, then here are top Spring interview questions which you must understand the solutions to:

Question: Please list down the numerous capabilities of Spring Framework and the advantages of the use of the identical.

Answer:

Features of Spring Framework:

Allows developing and coping with the configuration and lifecycle of application gadgets

AOP (Aspect Oriented Programming) affords assist for unified improvement by way of isolating software commercial enterprise good judgment from gadget services

Highly configurable MVC net utility framework with the capacity to without difficulty transfer to different frameworks

Instead of searching out or creating based gadgets, the gadgets provide their dependencies. This design principle is called IoC (Inversion of Control)

Lightweight

Offers a generic abstraction layer for transaction control that also can be utilized in container-less environments

The JDBC abstraction layer offers an exception hierarchy that simplifies error managing

Advantages:

Enables POJO (Plain Old Java Object) Programming that in addition enables non-stop integration and testability

Open-source and not using a supplier lock-in

Simplified JDBC because of DI (Dependency Injection) and Inversion of Control

Thanks to the layered architecture, it’s clean to maintain what you want and discard what you don’t

Question: How many modules does the Spring Framework have?

Answer: Spring Framework has about 20 modules. Each of them is assessed into one of the following layers:

Spring Core Container – The basic middle layer of the Spring Framework. It has the following modules:

SpEL (Spring Expression Language)

Spring Bean

Spring Core

Spring Context

Data Access/Integration – This is the layer answerable for providing help for interacting with the database. It incorporates the subsequent modules:

JDBC (Java DataBase Connectivity)

JMS (Java Messaging Service)

ORM (Object Relational Mapping)

OXM (Object XML Mappers)

Transaction

Web – Providing support for creating net programs is the duty of this layer. It has these modules:

Web

Web – MVC

Web – Portlet

Web – Socket

Aspect-Oriented Programming – Allows the use of Advice, Pointcuts, and others to decouple the code

Instrumentation – Provides assist for sophistication instrumentation and classloader implementations

Test – Responsible for presenting guide for engaging in checking out the use of JUnit and TestNG

Some additional Spring Framework modules are:

Aspects – Offers support for integrating with AspectJ

Messaging – Provides support for STOMP and an annotation programming model used for processing as well as routing STOMP messages from WebSocket clients

Spring Framework

Question: What additives does a Spring application have?

Answer: A regular Spring utility can be subdivided into the subsequent additives:

Bean Class – Contains homes, functions, setter and getter strategies, et cetera

Bean Configuration File – Contains information on classes in addition to the way to configure the identical

Interface – Defines the functions

Spring Aspect Oriented Programming – Provides the capability of move-reducing issues

User Program – Uses the function

Question: What do you recognize by using the Spring IoC Container? Explain their types.

Answer: The Spring IoC field lies at the middle of the Spring Framework. The field makes use of Dependency Injection for coping with numerous Spring utility components.

The IoC container is chargeable for developing the objects, configuring them, wiring them together, and coping with their lifecycle. The box gets commands approximately the same from the provided configuration metadata.

Means for imparting the configuration metadata can include Java annotations, Java code, or XML. There are two varieties of IoC containers in Spring:

ApplicationContext – Provides extra capability. It is built on top of the BeanFactory interface.

BeanFactory – A prepackaged elegance containing a set of beans. Instantiates the bean every time as required by way of the clients

Question: Please provide an explanation for the Dependency Injection in Spring. In how many methods can the same be used?

Answer: Instead of making gadgets directly, Dependency Injection allows defining how items ought to be created. As such, the code doesn’t at once contain connecting components and services together.

The configuration document has the information on which services are wanted via which additives. The IoC box is chargeable for connecting additives with the correct services. Dependency Injection may be used inside the following forms:

Construction Injection

Setter Injection

Question: Can you differentiate between ApplicationContext and BeanFactory in Spring?

Answer:

Annotation Based Dependency – BeanFactory doesn’t support annotation-based totally dependency at the same time as ApplicationContext does

Interface Definition – BeanFactory interface is defined in org.Springframework.Beans.Manufacturing unit.BeanFactory whilst the ApplicationContext interface is defined in org.Springframework.Context.ApplicationContext

Internationalization Support – While ApplicationContext helps internationalization, BeanFactory doesn’t

Object Management – BeanFactory makes use of syntax for offering a useful resource object. Contrarily, ApplicationContext creates in addition to manages aid items on its very own

Type of Initialization – ApplicationContext uses eager or aggressive initialization. On the alternative hand, BeanFactory makes use of lazy initialization

Question: How is the configuration metadata furnished to the Spring container?

Answer: There are three ways in which the configuration metadata is supplied to the Spring container, enumerated as follows:

Annotation-based totally Configuration – By default, annotation wiring is grew to become off in the Spring field. Using annotations at the relevant class, area, or approach announcement permits it to be used as a alternative of the use of XML for describing a bean wiring.

Java-based Configuration – This is the latest form of configuration metadata in Spring Framework. It has  essential components:

@Bean annotation – Same as that of the <bean/> detail

@Configuration annotation – Allows defining inter-bean dependencies through sincerely calling different @Bean techniques in the same @Configuration elegance

XML-primarily based Configuration – The dependencies, in addition to the offerings required via beans, are specified in configuration files that follow the XML format. Typically, these configuration documents comprise numerous software-specific configuration options and bean definitions.

Question: What do you understand by using Spring Beans? How many bean scopes are supported with the aid of Spring Framework?

Answer: Configured, instantiated, controlled, and stressed by means of the Spring IoC field, Spring Beans are the gadgets that form the core of a Spring application. Spring Beans are created with the configuration metadata provided to the Spring IoC field.

Spring Framework offers aid for a total of 5 scopes:

Global-session* – Provides scope for a bean definition to a Global HTTP-session

Prototype – Provides scope for a unmarried bean definition for having any wide variety of object times

Request* – Provides scope for a bean definition to an HTTP-request

Session* – Provides scope for the bean definition to a single instance according to Spring IoC box

Singleton – Provides scope for the bean definition to a single example in line with Spring IoC box * ? Available most effective while the usage of an internet-aware ApplicationContext.

Question: Please explain the Bean lifecycle in Spring Bean Factory Container?

Answer:

The bean lifecycle starts offevolved with the Spring IoC field instantiating the bean from the bean’s definition gift within the XML report

As unique inside the bean definition, Spring then populates all residences the usage of DI

If the bean implements the BeanNameAware interface then the setBeanName() approach is referred to as through passing the bean ID

If the bean implements the BeanFactoryAware interface, the setBeanFactory() method is known as by using passing an example of the bean

If there are any BeanPostProcessors related to the bean then the preProcessBeforeInitialization() methods are called

The init approach is known as if it's miles certain for the bean

At closing, the postProcessAfterInitialization() strategies are known as if there are any BeanPostProcessors associated with the bean.

Question: What is a Spring Configuration File?

Answer: Since Spring is based totally on the concept of Dependency Injection, all of the training, interfaces, and their dependencies are stored in a file termed as the spring configuration file. It is a .Xml record. The spring container makes use of this file to govern the lifecycle of spring beans. A bean is configured as:

<bean id = "..." class = "..." init-method = "..." lazy-init="true" destroy-method="....">
      <!-- bean dependencies and configurations -->
</bean>

Question: What are the advantages of IoC (Inversion of Control)?

Answer: The benefits are:

No need to write down considerable code on how services are created and getting object references. Everything can be completed thru easy configurations. New dependencies and services may be introduced just by adding a constructor or setter method.

Code is extra on hand to unit test as it is designed as several components, and builders can inject their gadgets and switch implementations.

Loose coupling of additives.

Allows for lazy loading of gadgets and dependencies.

Question: What is the Bean life cycle in Spring Bean Factory Container?

Answer: The bean lifecycle is as follows:

The bean is instantiated by the Spring box from the bean definition observed inside the spring configuration report.

Using dependency injection, all the residences exact in the bean definition are populated.

If the bean implements the BeanNameAware interface, setBeanName() is called via the bean manufacturing unit bypassing the bean's ID.

If the bean implements the BeanNameAware interface, setBeanFactory() is known as by using passing the bean's example. 

If any BeanPreProcessors are associated with the bean, preProcessBeforeInitialization(), techniques are called.

Init-method, if defined for the bean, might be known as.

Lastly, postProcessAfterInitialization() techniques are called, if required.

Question: Explain internal beans in Spring.

Answer: Inner beans are the beans that exist within the scope of another bean. The concept is just like internal classes in Java. The internal bean is defined because the target in the outer bean id tag.

<bean id = "outerBean" class = "...">
      <property name = "target">
         <bean id = "innerBean" class = "..."/>
      </property>
</bean>

Question: What is Annotation-based field configuration? Also, explain how to show on annotation wiring in Spring Framework.

Answer: Annotation-based totally box configuration is an opportunity to XML setups. Rather than the usage of XML for describing a bean wiring, the developer actions the configuration to the element elegance by using annotations on the ideal class, discipline, or method announcement.

Because annotation wiring is turned off with the aid of default, it wishes to be became on before it is able to be used. It is performed by means of configuring the <context:annotation-config/> element in the Spring configuration document.

Question: Please provide an explanation for the diverse annotations supported by using Spring.

Answer:

@Autowired – Used for autowiring bean on the setter strategies, a belongings, constructor or methods with arbitrary names or numerous arguments. It provides unique control over how and in which the autowiring desires to be executed.

@Component – A familiar stereotype for a Spring-controlled factor, it marks a Java class as a bean that can be picked up by using a issue-scanning mechanism and pull it into the software context.

@Controller – Marks a category as a Spring Web MVC controller. Beans marked with this annotation are automatically imported into the Dependency Injection box.

@Qualifier – Used along side @Autowired annotation for specifying that simplest one of the numerous but alike beans, wishes to be stressed out.

@Repository – A specialization of the thing annotation with nearly equal use and capability. Specifically, it presents extra advantages for DAOs (Data Access Objects).

@RequestMapping – Maps a selected HTTP request method to a specific magnificence or method in controller chargeable for handling the respective request.

@Required – Applied to bean property setter methods, it indicates that the bean belongings desires to be populated on the configuration time with the use of an explicit assets fee present in a bean definition or via autowiring. In case the bean belongings is not populated, the field throws the BeanInitializationException message.

@Service – Another specialization of the factor annotation. Although it doesn’t provide any additional behavior over the aspect annotation, it could be used over the @component annotation in service-layer training for specifying the intent in a better manner.

Question: What do you mean via Spring DAO aid?

Answer: The Spring DAO aid eases running with facts access technologies, including JDBC, JDO, and Hibernate, in a dependable manner. Also, it lets in coding with out traumatic about catching specific-technology exceptions and without problems makes a switch amongst patience technologies.

Question: What instructions does the JDBC API include?

Answer:

JdbcTemplate

NamedParameterJdbcTemplate

SimpleJdbcCall

SimpleJdbcInsert

SimpleJdbcTemplate

Question: How will you get right of entry to Hibernate the usage of Spring Framework?

Answer: Hibernate may be accessed using Spring Framework in the following two methods:

Extending HibernateDAOSupport after which applying an AOP Interceptor node

Inversion of Control with a Hibernate Template and Callback

Question: Enumerate the kinds of transaction management supported with the aid of Spring.

Answer: Spring Framework gives assist for two types of transaction management:

Declarative transaction control – While the transaction management is separated from the commercial enterprise code, only annotations or XML-primarily based configurations are used for managing transactions.

Programmatic transaction control – The transaction is managed with programming. Although extremely flexible, it is very difficult to keep.

Question: Please give an explanation for the AOP technique.

Answer: AOP or Aspect-Oriented Programming is a programming approach that lets in programmers to modularize conduct that cuts throughout the standard division of responsibility or move-slicing worries. Logging and transaction management are examples of go-reducing issues.

Question: What is Advice in Spring? Explain its diverse sorts.

Answer: Any motion taken by way of an thing at some precise joinpoint in Spring Framework is known as an Advice. Spring AOP uses advice for retaining a chain of interceptors “round” the joinpoint i.E. As an interceptor. Advice may be of the subsequent sorts:

After (sooner or later) – Configured the use of the @After annotation mark, it is achieved after a joinpoint technique, whether exiting typically or throwing an exception

After returning – Configured the usage of the @AfterReturning annotation mark, it is performed proper after the joinpoint technique completes ordinary execution

After throwing – Configured the use of the @AfterThrowing annotation mark, it is finished if and handiest if the jointpoint method exits with the aid of throwing an exception

Around – Configured the usage of the @Around annotation mark, it is performed earlier than as well as after a joinpoint approach

Before – Configured using the @Before annotation mark, it's far completed before the joinpoint technique

Question: Could you draw a assessment between issue and crosscutting issues in Spring AOP?

Answer: While the concern is a behavior that the developer desires to have in a particular module of a Spring application, the move-reducing problem is a difficulty that is relevant at some point of the complete Spring utility.

Question: What do you recognize by the Spring MVC framework?

Answer: The Spring MVC framework is liable for offering model-view-controller structure as well as geared up-to-use additives, used for developing flexible and loosely coupled web apps.

The MVC pattern enables in isolating out the numerous aspects of the application, such as business good judgment, input logic, and UI common sense, similarly to providing a unfastened coupling amongst these separated elements.

Question: Please explain DispatcherServlet.

Answer: The DispatcherServlet is the essence of Spring Web MVC framework and handles all the HTTP requests in addition to responses. Upon receiving the access 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 tests the configuration document for the entry of view resolver and calls the required view factor.

Question: What is Spring?

Answer: Spring is an integrated framework this is used for developing employer applications in Java language.

Question: Are there any variations among the Bean Factory and Application Context?

Answer: Yes, there are many differences between the Bean Factory and Application Context. These are stated as underneath:

Bean Factory Application Context
It is a basic container It is an advanced container
It has a limited interface. It extends the Bean Factory interface
It offers various fundamental facilities. It offers additional facilities than Bean Factory, including integration with Spring AOP, Message Resource Handling for i18n, and various others.

Question: What are the differences among the Constructor Injection and Setter Injection?

Answer: The variations among the Constructor Injection and Setter Injection can be said as under

Constructor Injection Setter Injection
It has a complete injection It has a partial injection
It does not override the setter property It overrides the constructor property when they both are defined.
It creates new Instances when modifications are required. It does not create a new Instance when the property value is changed.
It is better for many more properties than Setter Injection It is good for a few properties.

Question: Define Autowriting in Spring?

Answer: Autowiring in Spring allows the pc programmers to use bean mechanically with out writing specific injection good judgment. The various car writing modes consist of the subsequent.

No.

Byname.

ByType.

Constructor.

Question: What are the distinctive varieties of Bean scopes in Spring framework?

Answer: There are especially 5 sorts of Bean scopes in the Spring framework. These encompass the following.

Singleton.

Prototype.

Request.

Session.

Global session.

Question: What is a Pointcut in Spring?

Answer: Pointcut is an expression language of Spring AOP.

What are the extraordinary cutting-edge variations of Spring framework?

The modern day versions of the Spring framework are as follows.

Spring 2.5

Spring 3.0

Spring 4.0

Question: What is a JoinPoint?

Answer: A JoinPoint is a factor at some point of the execution of a application and represents the method execution. It includes the execution of a method or even dealing with of an exception.

Question: What are the variations between Spring AOP and AspectJ AOP?

Answer: There are several differences among Spring AOP and AspectJ AOP, which can be said as under:

Spring AOP AspectJ AOP
The proxy is done through Runtime weaving. Compile-time weaving is done through AspectJ Java tools
Method level PointCut is applied Field level PointCut is applied
It is based on DTD It is based on schema and annotation configuration

Question: What is a proxy in Spring Framework?

Answer: A proxy in the Spring framework is called the introduction of an item after applying advice to a specific target object. 

Question: When are the goal item and proxy gadgets the identical?

Answer: The goal object and proxy object are the identical within the case of client gadgets.

Question: What is weaving in the Spring framework?

Answer: Weaving in Spring framework is the method of linking a specific issue with different application types or objects so that an counseled item is created. It is done normally at some stage in Runtime.

Question: What is Spring Security?

Answer: Spring Security is a separate module inside the Spring framework that specializes in presenting authentication and authorization techniques that may be utilized in Java Applications.

Question: What is the Spring Boot?

Answer: Spring Boot is the call given to a undertaking which offers a pre-configured set of the framework, which is supposed to reduce the boilerplate configuration. It helps in getting spring packages up and strolling with lesser codes.

Question: What is Reactive Programming?

Answer: Reactive programming is a non-blocking off and occasion-pushed software. It scales with a small variety of threads. The backpressure is on the key factor, which ascertains that the producers do now not crush clients.

Question: What are the advantages of Reactive Programming?

Answer: There are various advantages of Reactive Programming, which consist of the following.

It facilitates in increasing the usage of computing sources, inclusive of multicore and multi-CPU hardware.

It enables in growing overall performance via a reduction within the serialization technique.

Question: What are the important traits of the Reactive device?

Answer: The essential traits of the Reactive system includes the following.

Message-driven.

Resilient.

Responsive.

Elastic.

Question: Define Spring Webflux?

Answer: Spring Web flux is a notably reactive internet framework of Spring. It acts as an alternative to Spring MVC and offers a noticeably scalable and non-blocking stack.

Question: Define Mono and Flux sorts?

Answer: Mono and Flux types, are both the reactor of the Spring Framework 5. The Mono represents the unmarried async value, even as the Flux represents the movement of async value. Together they help to put into effect the publisher interface, which is described truely in the reactive streams specs.

Question: What is the fundamental difference among Mono and Flux in Spring?

Answer: Mono implements the publisher and returns zero or maybe one detail at the same time as the Flux implements the publisher and go back N factors.

Question: What are the not unusual capabilities of Mono and Flux?

Answer: The not unusual capabilities of Mono and Flux consist of the subsequent.

They represent streams.

They can’t be performed with out consuming the circulation the use of the subscribe method.

They are immutable and may be known as again and again to create a new instance of Flux or Mono.

Question: Difference among the Web customer and Webtestclient?

Answer: The difference among the Web client and Webtestclient can be stated as follows.

Web client Webtestclient
Web client acts as a reactive client who performs non-blocking HTTP requests.  Webtestclient also acts as a reactive client that can be used in tests.
It can handle reactive streams with backpressure. It can bind directly to WebFlux application by applying mock request and response objects.
It can take advantage of JAVA 8 Lambdas. It can connect to any server over an HTTP connection.

Question: Do you think that Spring five like minded with older variations of JAVA?

Answer: No, Spring five is not well suited with the older versions of JAVA. It calls for at the least JAVA 8 for attaining compatibility.

Question: Can Spring Boot allow Spring MVC or Spring WebFlux within the identical software?

Answer: Yes, Spring Boot can permit either Spring MVC or Spring WebFlux within the same application but with the circumstance to use only one after the other. This is because MVC is a blockading paradigm, and WebFlux is a non-blocking off paradigm and as a result cannot be used collectively.

Question: Can Spring MVC run on Netty?

Answer: No, Spring MVC cannot run on Netty. 

Question: Can Spring five Integrate with the Jdk9 Modularity?

Answer: Yes, Spring five should integrate with the Jdk9 Modularity. This may be stated as follows.

Step 1: Creating a brand new magnificence:

package com.hello;
public class HelloWorld {
    public String sayHello(){
        return "HelloWorld";
    }
}

Step 2: Creating a brand new module:

module com.hello {
    export com.hello;
}

Step three: Creating a new Java Project:

module com.hello.client {
    requires com.hello;
}

Step four: Testing the new module:

public class HelloWorldClient {
    public static void main(String[] args){
        HelloWorld helloWorld = new HelloWorld();
        log.info(helloWorld.sayHello());
    }
}

Conclusion

So, that completes the listing of the pinnacle Spring interview questions. These questions will certainly assist you buckle up to your Spring interview.

Looking for extra Spring interview questions? Here is a exceptional udemy course for the equal: Spring Interview Questions Preparation Course.

Here is a fantastic e-book that lets you prepare properly for any programming interview: Cracking the Coding Interview: 189 Programming Questions and Solutions.

All the very first-rate!

Be certain to test out those exceptional Spring tutorials to similarly refine your Spring information.




CFG