Common interview questions for Spring

1. What is Spring?

  • Spring is a lightweight Inversion of Control (IoC) and Aspect-Oriented (AOP) container framework.
  • Spring framework is a layered architecture consisting of 7 defined modules.
  • Spring modules are built on top of the core container, which defines how beans are created, configured, and managed.

2. Spring module composition

  1. Spring Core: The core container provides the basic functions of the Spring framework. The main component of the core container is BeanFactory, which is the implementation of the factory pattern. BeanFactory uses the Inversion of Control pattern to separate the application's configuration and dependency specifications from the actual application code.
  2. Spring Context: Spring context is a configuration file that provides context information to the Spring framework. Spring context includes enterprise services such as JNDI, EJB, email, internationalization, validation and dispatch functions
  3. Spring AOP: Through configuration management features, the Spring AOP module directly integrates aspect-oriented functions into the Spring framework. Therefore, it is easy to use the Spring framework to manage any AOP-enabled object. The SpringAOP module provides transaction management services for objects in Spring-based applications. By using SpringAOP, you can integrate declarative transaction management into application management programs without relying on components.
  4. Spring DAO: The JDBC DAO abstraction layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code that needs to be written (such as opening and closing connections). SpringDAO's JDBC-oriented exceptions follow the common DAO exception hierarchy
  5. Spring ORM: Several ORM frameworks are inserted into the Spring framework to provide ORM object relational tools, including JDO, Hibernate and iBatis SQL Map. All of these comply with Spring's generic transaction and DAO exception hierarchy.
  6. Spring Web: The Web context module builds on the application context module and provides context for web-based applications. Therefore, Spring framework supports integration with Jakarta Struts. The web module also simplifies handling multipart requests and binding request parameters to domain objects.
  7. Spring MVC: The MVC framework is a full-featured MVC implementation for building web applications. Through strategic interfaces, the MVC framework becomes highly configurable, and MVC accommodates a large number of view technologies, including JSP, Velocity, Tiles, iText, and POI.

What is Spring IOC

  1. IOC container: It is actually equivalent to a map, which stores objects. We store objects by configuring beans or annotations in xml (@repository, @server, @controller, @component)
  2. Inversion of control: IOC container is not introduced. When we need an object, we need a new one. After introducing IOC, we put all the objects in the container. When we need an object, we can create it through the IOC container, changing from active to passive. The control of the object is handed over to the IOC (equivalent to a dating agency)
  3. Dependency injection: The IOC container dynamically injects certain dependencies into objects during runtime.

What is Spring AOP

  1. Encapsulate the cross-business logic in the program into an aspect, and then inject it into a specific logical business, which can enhance an object or method
  2. Related terms:
  • Joinpoint (Joinpoint): A joinpoint represents a point that can be inserted into an aspect during application execution. This point can be a method call or an exception thrown. In Spring AOP, the connection point is always a method invocation.
  • Pointcut (PointCut): Each program has multiple connection points. How to locate a connection point of interest requires positioning through pointcuts. For example, connection point-database record, point-cutting point-query condition
    point-cutting is used to limit the scope of Spring-AOP startup, usually we use expressions to set, so the key word is range
  • Enhancement (Advice): An enhancement is a piece of program code woven into the connection point of the target class. In Spring, things like BeforeAdvice also have location information.
  • The notification is the result of literal translation. I personally feel that it is more appropriate to call it "business enhancement". The comparison code is the related method defined by the interceptor. The notification is divided into the following types:
    • Pre-notification (before): do some operations before executing the business code, such as obtaining the connection object
    • Post notification (after): do some operations after executing the business code. It will be executed regardless of whether an exception occurs, such as closing the connection object.
    • Exception notification (afterThrowing): When an exception occurs after executing the business code, operations need to be performed, such as rolling back the transaction
    • Return notification (afterReturning), the operation that will be performed if there is no exception after executing the business code
    • Around notification (around), this transaction we are currently talking about does not have a corresponding operation, so we will not talk about it for now.
  • Target The business object that needs to be enhanced
  • Weaving: Weaving is the process of adding enhancements to specific connection points for the target class.
  • Aspect: An aspect is composed of a cut point and an enhancement. Spring AOP weaves the cross-cutting logic defined by the aspect into the connection point defined by the aspect.
    Insert image description here

What is the difference between BeanFactory and ApplicationContext?

They are two core interfaces of Spring, both of which can be used as Spring containers. ApplicationContext is a subclass of BeanFactory.

  1. BeanFactory is a lazy loading method to inject beans. The instantiation will only be loaded when the bean is used (slow speed, saving space)
  2. ApplicationContext will create all beans after the container is started (it takes up space and is fast)

おすすめ

転載: blog.csdn.net/ilvjiale/article/details/115358130