Home >Java >JavaInterview questions >2020 New Java Interview Questions-Spring

2020 New Java Interview Questions-Spring

王林
王林forward
2020-06-24 16:49:052278browse

The purpose of spring to be used is: to solve the complexity of enterprise application development. Spring is a lightweight control inversion and aspect-oriented container framework. Spring can configure and combine simple components into complex applications.

2020 New Java Interview Questions-Spring

(Related recommendations: java interview questions)

1. Why use spring?

1. Introduction

Purpose: Solve the complexity of enterprise application development

Function: Use basic JavaBean instead of EJB, and provide more enterprise applications Function

Scope: Any Java application

Simply put, Spring is a lightweight inversion of control (IoC) and aspect-oriented (AOP) container framework.

2. Lightweight

Spring is lightweight in terms of both size and overhead. The complete Spring framework can be distributed in a JAR file with a size of just over 1MB. And the processing overhead required by Spring is negligible. Furthermore, Spring is non-intrusive: typically, objects in a Spring application do not depend on specific Spring classes.

3. Inversion of Control

Spring promotes loose coupling through a technology called Inversion of Control (IoC). When IoC is applied, other objects that an object depends on are passed in passively, instead of the object itself creating or finding dependent objects. You can think of IoC as the opposite of JNDI - instead of the object looking for dependencies from the container, the container actively passes the dependencies to the object when it is initialized without waiting for the object to request it.

4. Aspect-oriented

Spring provides rich support for aspect-oriented programming, allowing the separation of application business logic and system-level services (such as auditing and transaction management) Conduct cohesive development. Application objects only implement what they are supposed to do - complete business logic - nothing more. They are not responsible for (or even aware of) other system-level concerns, such as logging or transaction support.

5. Container

Spring contains and manages the configuration and life cycle of application objects. In this sense, it is a container. You can configure how each of your beans is created—— Based on a configurable prototype, your bean can create a single instance or generate a new instance every time it is needed - and how they are related to each other. However, Spring should not be confused with traditional heavyweight EJB containers, which are often large, cumbersome, and difficult to use.

6. Framework

Spring can configure and combine simple components into complex applications. In Spring, application objects are composed declaratively, typically in an XML file. Spring also provides many basic functions (transaction management, persistence framework integration, etc.), leaving the development of application logic to you.

All of these Spring features enable you to write code that is cleaner, more manageable, and easier to test. They also provide basic support for various modules in Spring.

2. Explain what is aop?

AOP (Aspect-Oriented Programming, aspect-oriented programming) can be said to be the supplement and improvement of OOP (Object-Oriented Programming, object-oriented programming). OOP introduces concepts such as encapsulation, inheritance, and polymorphism to establish an object hierarchy to simulate a collection of common behaviors.

When we need to introduce public behavior to dispersed objects, OOP is powerless. In other words, OOP allows you to define relationships from top to bottom, but it is not suitable for defining relationships from left to right. For example, the logging function. Logging code tends to be spread horizontally across all object hierarchies, having nothing to do with the core functionality of the objects it's spread to.

The same is true for other types of code, such as security, exception handling, and transparent persistence. This kind of irrelevant code scattered everywhere is called cross-cutting code. In OOP design, it leads to the duplication of a large amount of code and is not conducive to the reuse of various modules.

The AOP technology is just the opposite. It uses a technology called "cross-cutting" to dissect the inside of the encapsulated object and encapsulate the public behaviors that affect multiple classes into a reusable module and name it "Aspect".

The so-called "aspect", simply put, is to encapsulate the logic or responsibilities that have nothing to do with the business but are commonly called by the business modules, so as to reduce the duplication of code in the system and reduce the coupling between modules. and facilitate future operability and maintainability.

AOP represents a horizontal relationship. If the "object" is a hollow cylinder, which encapsulates the properties and behavior of the object; then the aspect-oriented programming method is like a sharp knife. Cut open these hollow cylinders to get the message inside. The cut section is the so-called "aspect". Then it restored these cut sections with incredible skill, leaving no trace.

Using "cross-cutting" technology, AOP divides the software system into two parts: core concerns and cross-cutting concerns. The main process of business processing is the core concern, and the part that has little relationship with it is the cross-cutting concern.

One of the characteristics of cross-cutting concerns is that they often occur in multiple places of the core concern, and they are basically similar everywhere. Such as authority authentication, logging, and transaction processing. The role of Aop is to separate various concerns in the system, separating core concerns and cross-cutting concerns.

As Adam Magee, senior solution architect at Avanade, said, the core idea of ​​AOP is to "separate the business logic in the application from the common services that support it."

3. Explain what is ioc?

IOC is the abbreviation of Inversion of Control, and most books translate it as "Inversion of Control".

In 1996, Michael Mattson first proposed the concept of IOC in an article about exploring object-oriented frameworks. We have already talked a lot about the basic ideas of object-oriented design and programming, so we won’t go into details again. Simply put, it means decomposing complex systems into cooperating objects. After these object classes are encapsulated, the internal implementation is transparent to the outside. , thereby reducing the complexity of problem solving, and can be flexibly reused and extended.

The point put forward by the IOC theory is roughly this: using a "third party" to achieve decoupling between objects with dependencies. As shown below:

2020 New Java Interview Questions-Spring

As you can see, due to the introduction of the "third party" in the middle, that is, the IOC container, the four A, B, C, and D The objects no longer have a coupling relationship, and the transmission between gears all relies on the "third party". The control rights of all objects are handed over to the "third party" IOC container.

Therefore, the IOC container has become the key core of the entire system. It plays a role similar to "glue", binding all objects in the system together to function. Without this "glue" Mixture", objects will lose contact with each other, which is why some people liken the IOC container to a "glue".

Let’s do another experiment: remove the IOC container in the middle of the picture above, and then take a look at this system:

2020 New Java Interview Questions-Spring

What we see now The screen is everything we need to complete to implement the entire system. At this time, the four objects A, B, C, and D have no coupling relationship and have no connection with each other.

In this case, when you implement A, you no longer need to consider B, C, and D. The dependencies between objects have been reduced to a minimum. Therefore, if IOC containers can be realized, it will be a wonderful thing for system development. Each member participating in the development only needs to implement his own class and has nothing to do with others!

4. What are the main modules of spring?

The Spring framework has integrated more than 20 modules so far. These modules are mainly divided into core containers, data access/integration, Web, AOP (aspect-oriented programming), tools, messaging and test modules as shown in the figure below.

2020 New Java Interview Questions-Spring

5. What are the commonly used injection methods in spring?

Spring implements IOC (Inversion of Control) through DI (Dependency Injection). There are three main injection methods:

  • Constructor method injection

  • setter injection

  • Annotation-based injection

The above is the detailed content of 2020 New Java Interview Questions-Spring. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete