Summary of Spring Interview Frequently Asked Questions

1. General questions

1.1. What are the main functions of different versions of Spring Framework?

Version Feature
Spring 2.5 was released in 2007. This is the first version to support annotations.
Spring 3.0 was released in 2009. It fully leverages the improvements in Java5 and provides support for JEE6
.
Spring 4.0 was released in 2013. This is the first version to fully support JAVA8.

1.2. What is Spring Framework?

Spring is an open source application framework designed to reduce the complexity of application development. It is lightweight and loosely
coupled.
It has a layered architecture that allows users to select components while also providing a cohesive framework for J2EE application development. It can integrate other frameworks, such as Structs, Hibernate,
EJB, etc., so it is also called a framework of frameworks.

1.3. List the advantages of Spring Framework.

Due to the layered architecture of Spring Frameworks, users are free to choose the components they need.
Spring Framework supports POJO (Plain Old Java Object) programming, enabling continuous integration
and testability. JDBC is simplified thanks to dependency injection and inversion of control. It's open source and free.

1.4. What are the different functions of Spring Framework?

Lightweight - Spring is lightweight in terms of code size and transparency. IOC - Inversion of Control AOP - Aspect-oriented
programming can separate application business logic and system services to achieve high cohesion. Container - Spring is responsible
for creating and managing the life cycle and configuration of objects (Beans). MVC - provides a high
degree of configurability for web applications, and the integration of other frameworks is also very convenient. Transaction Management - Provides a common
abstraction layer for transaction management. Spring's transaction support can also be used in environments with fewer containers. JDBC Exceptions - Spring
's JDBC abstraction layer provides an exception hierarchy that simplifies error handling strategies.

1.5. How many modules are there in Spring Framework and what are they?

Insert image description here
Spring Core Container – This layer is basically the core of Spring Framework. It contains the following modules
:
 Spring Core
 Spring Bean
 SpEL (Spring Expression Language)
 Spring Context
Data Access/Integration – This layer provides support for interacting with the database. It contains the following modules:
 JDBC (Java DataBase Connectivity)
 ORM (Object Relational Mapping)
 OXM (Object XML Mappers)
 JMS (Java Messaging Service)
 Transaction
Web – This layer provides support for creating web applications. It contains the following modules:
 Web
 Web – Servlet
 Web – Socket
 Web – Portlet
AOP
 This layer supports aspect-oriented programming
Instrumentation
 This layer provides support for class detection and class loader implementation.
Test
 This layer provides support for testing using JUnit and TestNG.
Several miscellaneous modules:
Messaging – This module provides support for STOMP. It also supports an annotation programming model for
routing and handling STOMP messages from WebSocket clients.
Aspects – This module provides support for integration with AspectJ.

1.6. What is Spring configuration file?

Spring configuration files are XML files. This file mainly contains class information. It describes how these classes are
configured and introduced into each other. However, XML configuration files are verbose and cleaner. If not planned
and written correctly, managing large projects becomes very difficult.

1.7. What are the different components of a Spring application?

Spring applications generally have the following components:
 Interface - defines functionality.
 Bean class- It contains properties, setter and getter methods, functions etc.
 Spring Aspect-Oriented Programming (AOP) - Provides the functionality of aspect-oriented programming.
 Bean configuration file - Contains information about classes and how to configure them.
 User Program - It uses interfaces.

1.8. What are the ways to use Spring?

There are following ways to use Spring:
 As a full-fledged Spring web application.
 As a third-party web framework, use Spring Frameworks middle layer.
 For remote use.
 As an enterprise-level Java Bean, it can wrap existing POJOs (Plain Old Java
Objects).

2. Dependency injection (Ioc)

2.1. What is Spring IOC container?

The core of the Spring framework is the Spring container. Containers create objects, assemble them together, configure them
and manage their complete life cycle. The Spring container uses dependency injection to manage the components that make up an application
. The container receives instructions for object instantiation, configuration, and assembly by reading the provided configuration metadata
. This metadata can be provided via XML, Java annotations or Java code.
Insert image description here

2.2. What is dependency injection?

In dependency injection, you don't have to create objects, but you have to describe how to create them. Rather than wiring components and services together directly in code
, you describe which components require which services in a configuration file.
They are assembled together by the IoC container.

2.3. How many ways can dependency injection be completed?

Generally, dependency injection can be done in three ways, namely:
 Constructor injection
 Setter injection
 Interface injection
In Spring Framework, only constructor and setter injection are used.

2.4. Distinguish between constructor injection and setter injection.

Constructor injection setter injection
There is partial injection without partial injection.
The setter property will not be overwritten. The setter property will be overwritten.
Any modification will create a new instance. Any modification will not create a new instance. It
is suitable for setting many properties and is suitable for setting a small number of properties.

2.5. 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 when requested by the client .
ApplicationContext - The ApplicationContext interface extends the BeanFactory interface. It provides some additional functionality based on BeanFactory.

2.6. Distinguish between BeanFactory and ApplicationContext.

BeanFactory ApplicationContext
It uses lazy loading It uses just-in-time loading
It uses syntax to explicitly provide resource objects It creates and manages resource objects by itself
Does not support internationalization Supports internationalization
Does not support dependency-based annotations Supports dependency-based annotations

2.7. List some benefits of IoC.

Some of the benefits of IoC are:
 It will minimize the amount of code in the application.
 It will make your application easy to test as it does not require any singleton
or JNDI lookup mechanism in unit test cases.
 It promotes loose coupling with minimal impact and least intrusive mechanisms.
 It supports on-the-fly instantiation and lazy loading of services.
2.8. Spring IoC implementation mechanism.
The implementation principle of IoC in Spring is the factory pattern plus reflection mechanism.
Example:

interface Fruit {
public abstract void eat();
}
class Apple implements Fruit {
public void eat(){
System.out.println("Apple");
} }
class Orange implements Fruit {
public void eat(){
System.out.println("Orange");
} }
class Factory {
public static Fruit getInstance(String ClassName) {
Fruit f=null;
try {
f=(Fruit)Class.forName(ClassName).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return f;
} }
class Client {
public static void main(String[] a) {
Fruit f=Factory.getInstance("io.github.dunwu.spring.Apple");
if(f!=null){
f.eat();
} } }

Not finished yet, the article will continue...

Guess you like

Origin blog.csdn.net/mars131458/article/details/132103300