Home  >  Article  >  Java  >  'Interview Eight-Part Essay' Spring Volume 18

'Interview Eight-Part Essay' Spring Volume 18

Java后端技术全栈
Java后端技术全栈forward
2023-08-22 15:55:591186browse

"Spring Volume 18 of "Interview Eight-Part Essay" It's freshly released again. This time I have sorted out the interview questions about spring. I also looked through the interview questions about spring online and summarized them. , the basics are all here. Of course, there may be some concepts that are too basic. I have directly organized them into a certain question, so I have not opened a separate question. I wish everyone a smooth interview~



  • 1.What design patterns are used in spring?

  • 2.What are the core modules in spring?

  • 3. Tell me what you understand by IOC?

  • 4.What are the IOC containers in spring? What are the differences?

  • 5. What is the difference between BeanFactory and FactoryBean?

  • 6. What is the difference between @Repository, @Service, @Compent, and @Controller?

  • 7. So what is DI?

  • 8. What is AOP?

  • ##9. What is the difference between dynamic proxy and static proxy?

  • 10. What is the difference between JDK dynamic proxy and CGLIB proxy?

  • 11.What is the difference between Spring AOP and AspectJ AOP?

  • 12.What is the life cycle of Bean in spring?

  • 13.How does spring solve circular dependencies?

  • 14. Why use third-level cache? Can’t the second-level cache solve it?

  • 15. What is the difference between @Autowired and @Resource?

  • 16.What are the transaction isolation levels of spring?

  • 17.What are the propagation mechanisms of spring transactions?

  • 18.springBoot automatic assembly principle?



1.What design patterns are used in spring?

'Interview Eight-Part Essay' Spring Volume 18
  • 『1.Factory Design Pattern』: For example, producing Bean objects through BeanFactory and ApplicationContext
  • 『2.Agent Design Pattern』: AOP The implementation method is through proxy. Spring mainly uses JDK dynamic proxy and CGLIB proxy
  • 「3. Singleton design pattern」: Bean in Spring The default is singleton
  • 「4. Template method mode」: In Spring, jdbcTemplate and other classes ending with Template that operate on the database will use templates Method design pattern, some common functions
  • 「5. Wrapper design pattern」: Our project needs to connect to multiple databases, and different customers need to connect to each database. During each visit, different databases will be accessed as needed.This mode allows us to dynamically switch between different data sources according to customer needs
  • 「6. Observer Mode」: Spring event-driven model observer mode ’s
  • 「7. Adapter Mode」: Spring AOP’s enhancement or notification (Advice) uses the adapter mode

2.What are the core modules in spring?

'Interview Eight-Part Essay' Spring Volume 18
  • 1.「Spring Core": Spring core, which is the most basic part of the framework, provides IOC and dependency injection DI features
  • 2."Spring Context": Spring Context container, which is a sub-interface with enhanced functionality of BeanFactory
  • 3.「Spring Web」: It provides support for Web application development
  • 4.「Spring MVC」: It is aimed at the implementation of MVC ideas in Web applications
  • 5.「Spring DAO」: Provides a JDBC abstraction layer, simplifying JDBC coding, and at the same time, the coding is more robust
  • 6.「Spring ORM」: It supports Integration of popular ORM frameworks, such as: Spring Hibernate, Spring iBatis, Spring JDO integration, etc.
  • 7.「Spring AOP」: Aspect-oriented programming, It provides a programming implementation compatible with the AOP Alliance

3. Tell me what IOC you understand?

'Interview Eight-Part Essay' Spring Volume 18

First of all, IOC is a "container" , which is used to load objects. Its core idea is "Inversion of Control"

So what exactly"What is inversion of control"?

Inversion of control means,"Give control of the object to spring and manage it by the spring container 』, we will not perform any operations

Then why"What needs inversion of control"?

Let's imagine that when there is no inversion of control, we need"to create objects and configure objects ourselves" , and "manually handle various complex dependencies between objects" , when a project is scaled up, the maintenance of this relationship is very troublesome. So there is the concept of inversion of control, which leaves a series of operations such as object creation and configuration to spring for management. When we use it, we just need to get it

4.What are the IOC containers in spring? What are the differences?

spring mainly provides "two IOC containers" , one is "BeanFactory" ", there is another one is "ApplicationContext"

The difference between them is that BeanFactory "only provides the most basic functions of instantiating objects and getting objects" , and ApplicationContext is a product derived from BeanFactory and is its subclass. Its function is more powerful, such as supporting annotation injection, internationalization and other functions

5. What is the difference between BeanFactory and FactoryBean?

These two are "different products"

"BeanFactory is IOC Container" is used to carry objects

"FactoryBean is an interface", which provides a more flexible way for Bean. By proxying a Bean object, the method can be processed before and after. some operations.

6. What are the differences between @Repository, @Service, @Compent and @Controller?

of these four annotations "The essence is the same. The object identified by the annotation is put into the spring container, just to distinguish different application layers in use"

  • @Repository:dao layer
  • @Service:service layer
  • @Controller:controller layer
  • @Compent: Other components that do not belong to the above three layers use this annotation

7. Then DI What is it?

DI is dependency injection. In fact, it is roughly the same as IOC, but "the same concept is explained from different angles"

What DI describes"The focus is on dependence", we said"The core function of IOC is to dynamically provide other dependent objects to an object when the program is running", This function is accomplished by relying on DI. For example, if we need to inject an object A, and this object A depends on an object B, then we need to inject this object B into object A. This is dependency injection

## There are three injection methods in #spring

  • Interface injection
  • Constructor injection
  • set injection

8. What is AOP?

AOP means:

"Aspect-oriented programming, A technology that achieves unified maintenance of program functions through precompilation and dynamic proxies during runtime".

AOP is

"The continuation of OOP (Object-Oriented Programming)". It is an important content in the Spring framework and a derivative paradigm of functional programming. AOP can be used to isolate various parts of business logic, thereby reducing the coupling between various parts of business logic, improving program reusability, and improving development efficiency.

「AOP implementation is mainly divided into two categories:」

'Interview Eight-Part Essay' Spring Volume 18

  • 「Static AOP implementation」, AOP framework「During the compilation phase」Modify the program source code, A static AOP proxy class is generated (the generated *.class file has been changed and a specific compiler needs to be used), such as AspectJ
  • "Dynamic AOP Implementation" , AOP framework "at runtime" For dynamically generating proxy objects (using JDK dynamic proxy in memory, or dynamically generating AOP proxy classes using CGlib), such as SpringAOP

The implementation of AOP in spring is "implemented through dynamic proxy". If the interface is implemented, the JDK dynamic proxy will be used, otherwise the CGLIB proxy will be used.

'Interview Eight-Part Essay' Spring Volume 18

「There are 5 notification types:」

  • 「@Before」: Notify before the target method is called
  • ##「@AfterReturning」: Call after the target method returns or exceptions
  • 『@AfterThrowing』: Called after the target method returns
  • 『@After』: Called after the target method exceptions
  • 『@Around』: Encapsulate the target method and determine the calling time yourself

9. What is the difference between dynamic proxy and static proxy?

「Static proxy」

  • Created by programmers or automatically generated by specific tools, and then compiled. Before the program is run, the .class file of the proxy class already exists
  • Static proxies usually only proxy one class
  • Static proxies know in advance What to proxy

"Dynamic proxy"

  • When the program is running, it is dynamically created using the reflection mechanism. Cheng
  • Dynamic proxy is a proxy for multiple implementation classes under an interface
  • Dynamic proxy does not know what to proxy, only at runtime Only then did I know

10. What is the difference between JDK dynamic proxy and CGLIB proxy?

JDK dynamic proxy business class "must implement a certain interface" , which is "implemented based on a reflection mechanism" , and generates an implementation A proxy class of the same interface, and then enhances the code by overriding methods.

CGLIB dynamic proxy uses the bytecode processing framework ASM. Its principle is to use bytecode technology to "create a subclass for a class, and then override the method of the parent class", to achieve Enhancements to the code.

11.What is the difference between Spring AOP and AspectJ AOP?

Spring AOP is a run-time enhancement, which is implemented through

"dynamic proxy implementation"

AspectJ AOP is a compile-time enhancement, which requires a special compiler. Completion is achieved by

"modifying the code", and supports "three weaving methods"

  • 「Compile-time weaving」: It is to weave related proxy classes into the bytecode when compiling the bytecode
  • "Weaving after compilation": After compiling the initial class, it is found that AOP enhancement is needed, and then the relevant code is woven into it
  • "Weaving when the class is loaded" : refers to weaving
  • when the loader loads the class.
'Interview Eight-Part Essay' Spring Volume 18
#Main differencesSpring AOPAspecjtJ AOPEnhancement methodRun-time enhancementCompile-time enhancementImplementation methodDynamic proxyModify codeCompilerjavacSpecial compiler ajcEfficiencyLower (runtime reflection loss performance)HigherWeaving methodRuntimeCompile time, after compilation, class loading time

12.What is the life cycle of Bean in spring?

The SpringBean life cycle is roughly divided into 4 stages:

'Interview Eight-Part Essay' Spring Volume 18
  • 1.「instantiation」, instantiate the Bean object
  • ##2.
    「Fill attributes」, assign a value to the Bean
  • 3.
    「Initialization」
    • If the Aware interface is implemented, container resources will be obtained through its interface
    • If the BeanPostProcessor interface is implemented, the pre- and post-processing enhancements of this interface will be called back
    • If the init-method method is configured, ] will execute this method
    ##4.
  • 「Destruction」
    • If the DisposableBean interface is implemented, the destroy method of the interface will be called back
    • If the destroy-method method is configured, the destroy method will be executed -method configuration method

13. How does spring solve circular dependencies?

'Interview Eight-Part Essay' Spring Volume 18Circular dependency means that two objects are dependent on each other, forming a circular call link

spring uses the third-level cache to solve the circular dependency. The core logic is Separate the steps of instantiation and initialization, and then put them in the cache for another object to call

  • 「First-level cache」: Used to save objects that have been instantiated and initialized
  • 「Sec. Second-level cache": Used to save objects that have been instantiated but not initialized
  • "Third-level cache": Used to save an Object factory provides an anonymous internal class for creating objects in the second-level cache

The general process when a circular reference occurs between classes A and B

  • 1.A After completing the instantiation, go to"Create an object factory and put it into the third-level cache"
    • ##If A is proxied by AOP, then what is obtained through this factory is the object after A proxy
    • If A is not proxied by AOP, then what is obtained by this factory is the instantiation of A Object
  • #2. When A performs attribute injection, go to
    "Create B"
  • ##3. B performs attribute injection and requires A, then
  • "Get the A factory proxy object from the third-level cache"
    and inject it, then delete the A factory in the third-level cache and put the A object into the second-level cache
  • 4.B Complete subsequent attribute injection until the initialization is completed, and put B into the first-level cache
  • 5.
  • 「A from Get B from the first-level cache and inject B"
    , until the subsequent operations are completed, A is deleted from the second-level cache and placed in the first-level cache, and the circular dependency ends
spring has two prerequisites for solving circular dependencies:
  • 1."Not all the constructor method" circular dependency (otherwise the initialization and instantiation operations cannot be separated)
  • 2.「Must be a singleton」(Otherwise it cannot be guaranteed to be the same object)

14. Why use level three Cache, can't the second-level cache solve it?

Yes, the function of the third-level cache is to generate proxy objects in advance only when circular dependencies actually occur, otherwise it will only "create a Factory and put it into the third-level cache", but the object will not be actually created through this factory.

If you use the second-level cache to solve the circular dependency, it means that all beans must complete the AOP proxy after instantiation, which "violates the principles of Spring design" , Spring was designed at the beginning It is to complete the AOP proxy in the last step of the Bean life cycle, instead of performing the AOP proxy immediately after instantiation.

15. What is the difference between @Autowired and @Resource?

  • 「@Resource It is Java's own annotation." , @Resource has two attributes that are more important, namely name and type; Spring parses the name attribute of the @Resource annotation into the name of the bean, and the type attribute parses into the type of the bean. . So if the name attribute is used, the byName automatic injection strategy is used, and when the type attribute is used, the byType automatic injection strategy is used. If neither the name nor the type attribute is specified, the byName automatic injection strategy will be used through the reflection mechanism.

  • 「@Autowired is an annotation of spring」, introduced in spring2.5 version, Autowired only injects based on type,「Will not match name」. If the type cannot identify the injected object, it needs to be decorated with @Qualifier or @Primary annotations.

16.What are the transaction isolation levels of spring?

'Interview Eight-Part Essay' Spring Volume 18
  • DEFAULT: Use the DB default transaction isolation level
  • READ_UNCOMMITTED: Read uncommitted
  • READ_COMMITTED: Read committed
  • REPEATABLE_READ: repeatable read
  • SERIALIZABLE: serialization

17.What are the propagation mechanisms of spring transactions?

'Interview Eight-Part Essay' Spring Volume 18
  • ##1.
    『propagation_required』
    • Current method
      "Must be run in a context with a transaction", if there is a client transaction in progress, then the called end will be in the transaction Run, otherwise restart a transaction.(If an exception occurs on the called side, both the calling side and the called side transactions will be rolled back)
  • 2.『propagation_supports』
    • The current method does not need to have a transaction context, but it can run in a transaction if there is one
  • 3.『propagation_mandatory』
    • Indicates that the current method "must be run in a transaction", if there is no transaction, it will Throws an exception
  • 4.『propagation_nested』
    • If the current method has a If the transaction is running, the method should "run in a nested transaction". The nested transaction can be committed or rolled back independently of the encapsulated transaction. If the encapsulated transaction exists and the outer transaction throws an exception to rollback, then the inner transaction must be rolled back. On the contrary, the inner transaction does not affect the outer transaction. If the encapsulated transaction does not exist, it is the same as propagation_required
  • 5.『propagation_never』
    • The current service should not be run in a transaction. If "there is a transaction, an exception is thrown"
  • ##6.
    「propagation_requires_new」
    • Current method
      「Must run in its own transaction」. A new transaction will be started, and if there is an existing transaction running, this method will be suspended during runtime until the new transaction is committed or rolled back.
  • 7.
    『propagation_not_supported』
    • Methods should not be run within a transaction."If a transaction is running, it will be suspended during the running period, and execution will not be resumed until the transaction is committed or rolled back"

18. SpringBoot automatic assembly principle?

'Interview Eight-Part Essay' Spring Volume 18
  • 1. The container will Call the selectImports method of EnableAutoConfigurationImportSelector.class"Get a comprehensive list of commonly used BeanConfiguration"

  • 2. Spring-boot- will be read later spring.factories below autoconfigure.jar, "Get the fully qualified names ClassName of all Spring-related beans"

  • 3. Then continue"Call filter to filter one by one", filter out some beans that we don't need and do not meet the conditions

  • 4.Finally The qualified BeanConfiguration injects the property value in the default EnableConfigurationPropertie class and "injects it into the IOC environment"

The above is the detailed content of 'Interview Eight-Part Essay' Spring Volume 18. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Java后端技术全栈. If there is any infringement, please contact admin@php.cn delete