1. What is the Spring Framework?

The Spring Framework refers to the Spring Framework, which is a collection of modules that can be used to facilitate development. These modules are: core container, data access/integration, Web, AOP (aspect oriented programming), tools, messages, and test modules. For example, the Core component in Core Container is the Core of all Spring components, the Beans component and Context component are the basis for IOC and dependency injection, and the AOP component is used to implement tangential programming.

The Spring website lists six features of Spring:

Core technologies: Dependency Injection (DI), AOP, Events, Resources, I18N, validation, data binding, type conversion, SpEL. Testing: Mock objects, TestContext framework, Spring MVC tests, WebTestClient. Data access: transaction, DAO support, JDBC, ORM, marshalling XML. Web support: Spring MVC and Spring WebFlux Web Framework. Integration: remote processing, JMS, JCA, JMX, email, tasks, scheduling, caching. Languages: Kotlin, Groovy, dynamic languages.

2. Spring transaction propagation mechanism? There are seven types REQUIRED, SUPPORTS, MANDATORY, REQUIRES_NEW, NOT_SUPPORTED, NEVER, and NESTED

1, the REQUIRED:

2, SUPPORTS:

3, MANDATORY:

4, the REQUIRES_NEW:

5, NOT_SUPPORTED:

6, NEVER:

7, NESTED:

2. List some important Spring modules.

Spring Core: Basic, it can be said that all of Spring’s other functions depend on this library. It mainly provides IOC dependency injection function.

Spring Aspects: This module provides support for integration with AspectJ.

Spring AOP: AOP(Aspect Oriented Programming).

Spring JDBC: Java database connection.

Spring JMS: Java messaging service.

Spring ORM: Used to support ORM tools such as Hibernate.

Spring Web: Provides support for creating Web applications.

Spring Test: Provides support for JUnit and TestNG tests.

3. Talk about your understanding of Spring IoC and AOP

IoC

IoC is a design concept that hands over Control of manually created objects in programs to the Spring framework. IoC is also used in other languages, not unique to Spirng. IoC container is the carrier used by Spring to implement IoC. IoC container is actually a Map (key, value), which stores various objects.

The interdependencies between objects are managed by the IOC container and the object injection is completed by the IOC container. This greatly simplifies application development and frees applications from complex dependencies. The IOC container is like a factory. When we need to create an object, all we need to do is configure the configuration file/annotations, regardless of how the object was created. In a real project, a Service class might have hundreds or even thousands of classes as its underlying class. If we need to instantiate the Service, you might have to figure out the constructors of all the underlying classes of the Service every time, which can drive people crazy. With IOC, all you need to do is configure it and then reference it where you need it, which makes your project much more maintainable and easier to develop.

In the Spring era, we used to configure beans in XML files, but later developers decided that XML files were not a good way to configure beans, so SpringBoot annotation configuration slowly became popular.

AOP

AOP(aspect-oriented Programming: section-oriented Programming) can encapsulate the logic or responsibility (such as transaction processing, log management, permission control, etc.) that has nothing to do with business but is called jointly by business modules, so as to reduce the repeated code of the system and reduce the coupling degree between modules. And is conducive to the future scalability and maintainability.

Spring AOP is based on dynamic proxies. If you want to Proxy an object that implements an interface, Spring AOP uses JDK Proxy to create Proxy objects. If you don’t implement an interface, you can’t use JDK Proxy to Proxy objects. Spring AOP then uses Cglib to generate a subclass of the proxied object as the proxy, as shown in the following figure:

You can also use AspectJ, which is already integrated with Spring AOP and is arguably the most complete AOP framework in the Java ecosystem.

With AOP, we can abstract out some common functions and use them where needed, which greatly simplifies the amount of code. It is also convenient when we need to add new features, which also improves system scalability. Logging capabilities, transaction management, and other scenarios use AOP.

What is the difference between Spring AOP and AspectJ AOP?

Spring AOP is runtime enhancement, while AspectJ is compile-time enhancement. Spring AOP is based on Proxying, while AspectJ is based on Bytecode Manipulation.

Spring AOP already integrates with AspectJ, which is arguably the most complete AOP framework in the Java ecosystem. AspectJ is more powerful than Spring AOP, but Spring AOP is relatively simple,

If we have fewer cuts, there is not much difference in performance. However, when there are too many facets, AspectJ is the best choice, which is much faster than Spring AOP.

5. What are the scopes of beans in Spring?

Singleton: A unique bean instance. Beans in Spring are singleton by default. Prototype: Each request creates a new bean instance. Request: Each HTTP request generates a new bean that is valid only within the current HTTP Request. Session: Each HTTP request generates a new bean that is valid only for the current HTTP session. Global-session: the global session scope, which is only meaningful in portlet-based web applications, is no longer available in Spring5. Portlets are small Java Web plug-ins that can generate snippets of semantic code, such as HTML. They are based on portlet containers and can handle HTTP requests like servlets. However, unlike servlets, each portlet has a different session.

Do you understand the thread-safety issues of singleton beans in Spring?

Most of the time we don’t use multithreading in our systems, so few people pay attention to it. Singleton beans have threading problems, mainly because of thread-safe writes to non-static member variables of an object when multiple threads are working on the same object.

There are two common solutions:

Try to avoid defining mutable member variables in Bean objects (impractical).

Define a ThreadLocal member variable in the class, and store the required variable member variables in the ThreadLocal (the recommended approach).

7. Bean lifecycle in Spring?

8. What do you know about Spring MVC?

MVC is a design pattern, and Spring MVC is an excellent MVC framework. Under Spring MVC, we generally divide back-end projects into Service layer (processing business), Dao layer (database operation), Entity layer (Entity class), and Controller layer (control layer, returning data to the front page).

A simple schematic of Spring MVC looks like this:

9. Do you know the working principle of Spring MVC?

Here’s a bit of a typo: Spring MVC’s entry function, the front-end controller DispatcherServlet, is used to receive requests and respond to results.

Process Description (Important) :

1. The client (browser) sends the request directly to the DispatcherServlet(front-end controller). 2. According to the request information, the DispatcherServlet invokes the HandlerMapping (processor mapper) to parse the Handler corresponding to the request. 3. After being parsed to the corresponding Handler, it is processed by the HandlerAdapter. 4. The HandlerAdapter calls the actual Handler to handle the request and the corresponding business logic. 5. After processing the business, the processor will return a ModelAndView object. Model is the returned data object, and View is a logical View. The ViewResolver will find the actual View based on the logical View. 7. DispaterServlet passes the returned Model to the View (View rendering). 8. Return the View to the requester (browser)

What design patterns are used in the Spring framework?

Factory Design pattern :Spring uses the factory pattern to create bean objects from the BeanFactory, ApplicationContext. Proxy design pattern: Implementation of Spring AOP functionality. Singleton design pattern: Beans in Spring are singleton by default. Template method pattern :Spring JDBC Template, hibernateTemplate, and other classes that end in Template for database operations use the Template pattern. Wrapper design pattern: Our project needs to connect to multiple databases, and different customers need to access different databases on each visit. This pattern allows us to dynamically switch between different data sources based on customer needs. Observer Pattern: The Spring event-driven model is a classic application of the Observer pattern. Adapter pattern: The adapter pattern is used in Spring AOP enhancements or Advice, and is used in Spring MVC to adapt controllers.

What’s the difference between @Component and @bean?

The @Component annotation applies to classes, while the @bean annotation applies to methods. The @Component is automatically detected and assembled into the Spring container via classpath scanning (we can use the @ComponentScan annotation to define the path to be scanned to find classes that identify the classes that need to be assembled into the Spring bean container). The @bean annotation is usually defined to produce the Bean in a method marked with the annotation. The @bean tells Spring that this is an example of a class and gives it back to me when I need to use it. 3. The @Bean annotation is more customizable than the Component annotation, and in many places we can only register beans through the @Bean annotation. For example, when we reference a class from a third-party library that needs to be assembled into the Spring container, we can only do so through @Beans.

What are the annotations that declare a class as a Spring bean?

We typically use @AutoWired annotation auto-assembly beans. To identify a class as a class that can be used with an Autowired annotation auto-assembly bean, use the following annotation:

Component: Generic annotation to mark any class as a Spring Component. If a Bean does not know which layer it belongs to, it can be annotated using the @Component annotation. @repository: Corresponds to the persistence layer (Dao layer), which is used for database-related operations. @service: corresponds to the Service layer, which involves complex logic and requires the Dao layer. @Controller: corresponding to the Spring MVC control layer, the main user accepts the user request and calls the Service layer to return data to the front-end page.

13. How many ways does Spring manage transactions?

Programmatic transactions, hard-coded in code. (Not recommended) Declarative transactions. Configure (recommended) declarative transactions in configuration files. There are two types of declarative transactions:

Xml-based declarative transactions Declarative transactions based on annotations

14. What kinds of transaction propagation behavior do Spring transactions have?

Five constants representing the isolation level are defined in the TransactionDefinition interface:

1, TransactionDefinition. ISOLATION_DEFAULT: use a backend database default isolation level, Mysql default using REPEATABLE_READ isolation level, Oracle READ_COMMITTED) isolation level used by default. 2, TransactionDefinition. ISOLATION_READ_UNCOMMITTED: the lowest isolation level, allowing read has not yet been submitted data changes, may lead to dirty reads, phantom read or not repeatable read. 3, TransactionDefinition. ISOLATION_READ_COMMITTED: allow read of concurrent transactions have to submit data, can prevent dirty reads, but phantom read or not repeatable read could still happen. 4, TransactionDefinition. ISOLATION_REPEATABLE_READ: read many times on the same field results are consistent, unless the data have been modified by itself affairs on their own, can prevent the dirty read and not repeatable read, but phantom read could still happen. 5, TransactionDefinition. ISOLATION_SERIALIZABLE: the highest level of isolation, completely obey the ACID isolation level. All transactions are executed one by one so that interference between transactions is completely impossible, meaning that this level prevents dirty reads, unrepeatable reads, and phantom reads. But this severely affects the performance of the program and is not usually used.

What are Spring’s loop dependencies? How to solve it?

The Bean with cyclic dependencies must be a singleton. The dependency injection approach cannot be all constructor injection

Bean A depends on bean B while B depends on A.

How does Spring address loop dependencies?

Spring addresses cyclic dependencies with a three-level cache of singletonObjects, earlySingletonObjects, and singletonFactories.

When A, B two kind of circular reference occurs, after A complete instantiation, just use the instantiated object to create an object factory, and added to the three-level cache, if A is AOP agent, then through access to the factory is A proxy objects, if A is not AOP agent, then get to the factory is A instantiation of the object.

When A does property injection, B will be created, and B will depend on A, so when B is created, getBean(A) will be called to get the required dependency, and getBean(A) will get the required dependency from the cache:

The first step is to obtain the factory in the level 3 cache.

The second step is to call the getObject method of the object factory to get the corresponding object, which is then injected into B. B then goes through its lifecycle, including initialization, post-processor, and so on.

After B is created, B is injected into A, and A completes its life cycle. At this point, the loop dependency ends!