Spring’s nanny-level guide will teach you how to deal with the interviewer easily

Spring’s nanny-level guide will teach you how to deal with the interviewer easily

1.What is Spring? What is the function?

Spring is a lightweight JavaEE framework that mainly solves complex problems in enterprise applications. The Spring framework has three core parts: IoC container, AOP and data access/integration layer. The IoC container in Spring provides a mechanism for object creation and relationship management between objects to achieve loose coupling and scalability. AOP provides a good way to implement the processing of horizontal concerns, such as transaction management, security checks, caching, etc. The data access/integration layer provides many implementations of different data persistence technologies, such as JDBC, ORM and NoSQL.
Human words: spring is a lightweight open source free IOC and AOP container framework. IOC: Inversion of Control, AOP: Aspect-Oriented Programming, mainly to solve the problem of direct coupling between the business logic layer of enterprise-level application development and other layers of objects and objects, reduce development difficulty, and improve development efficiency. Better inherit various frameworks such as mysql, etc. You only need to import dependencies through the pom file to inherit the required jar package with one click

2. What do you understand by IoC (Inversion of Control)?

IoC is a design idea that achieves loose coupling by handing over dependencies between objects to containers for management. In Spring, the IoC container plays a central role, responsible for creating objects, assembling dependencies between objects, and managing their entire life cycle. IoC also provides some advanced features, such as AOP and event-driven, which can further enhance the maintainability and scalability of applications.

Human words: IOC is Inversion of Control. The most important thing is the container. The container manages the life cycle of the bean and controls the dependency injection of the bean. In layman's terms, the new process is handed over to the container to create and manage the life cycle of the object. If I want to create a new object, I can use DI (Dependency Injection) @Autowired to automatically inject the object. The implementation mechanism of ioc is factory mode plus reflection

3. What is AOP (Aspect Oriented Programming)?

AOP is a programming paradigm that can dynamically insert code into the flow of the original code during program running, thereby realizing the processing of horizontal concerns, such as logging, transaction management, and security checks. In Spring, AOP is implemented through the proxy mode. When a bean is created, a proxy object is generated for the bean. The proxy object can intercept calls to specified methods and perform specific operations before or after execution.

AOP usually becomes aspect-oriented programming, which is to extract and encapsulate some codes that have nothing to do with the business. In addition, a large number of repeated codes in some codes can use AOP to achieve the effect of repeated reuse and greatly improve the code's reusability. Improve readability and code robustness, and reduce code coupling. AOP is generally used in permission authentication, logging, transaction processing and other scenarios.
(1) Aspect: Specified in Spring Aop is the "aspect class", and the aspect class will manage pointcuts and notifications.
(2) Join point: Specify the enhanced business method
(3) Advice: It needs to be added to the business method There are many types of notifications that can be executed at different locations of the business methods that need to be added
(pre-notification, post-notification, exception notification, return notification, surround notification) a> (6) Weaving: The weaving method used by spring aop: dynamic proxy. The process of creating a dynamic proxy for the target object is called weaving (5) Target object (Target Object): Specify an enhanced object
(4) Pointcut: He decides which methods need to be enhanced and which do not need to be enhanced, and implements them in combination with pointcut expressions

4.What is the dependency relationship between beans in Spring?

In Spring, dependencies between beans are mainly realized through constructor injection, Setter method injection and automatic injection. Constructor injection is achieved by injecting other Beans as parameters through the Bean's constructor method; Setter method injection is used to inject other Beans through the Bean's Setter method; automatic injection is through the IoC container to automatically discover and inject other Beans when the Bean is created. This is done by setting the @Autowired or @Resource annotation.

Human words: In Spring, dependencies between beans can be achieved through constructor injection and property injection. Constructor injection means that when a bean requires another bean, Spring will automatically pass the reference of the dependent bean to the constructor of the other bean. In this way, dependency injection is completed when creating the Bean. Property injection means that when a bean requires another bean, Spring will automatically call the Setter method of the other bean and pass the reference of the dependent bean to the method. In this way, dependency injection is completed after the bean is created.

5. How is the singleton pattern implemented in the Spring framework?

In Spring, all beans are singletons by default. When the IoC container creates a bean, it caches an instance of the bean and returns the same instance on subsequent requests. This mechanism can improve application performance and reduce memory consumption, but it also requires attention to issues such as thread safety and state management.

After answering this question directly, I will continue to say that the simplest way to ensure the thread safety of a bean is to change the scope of the bean and change "singleton" to "prototype". In this way, requesting a bean is equivalent to new Bean(), so Thread safety can be guaranteed. Secondly, it is thread-unsafe only when the bean is stateful. I have highlighted it in the previous article. Those who are interested can take a look.

6.How is transaction management implemented in Spring?

Spring uses AOP to dynamically cut transaction-related logic into specified methods at runtime to achieve transaction management. Typically, Spring uses declarative transaction management to manage transactions. Developers can define transaction scope, propagation attributes, rollback rules, etc. through @Transactional annotation or XML configuration file.

7.What are the advantages of Spring?

The Spring Framework offers many advantages, including:

  1. Lightweight: The Spring framework is lightweight and does not need to rely on too many third-party libraries, making applications more efficient.

  2. Loose coupling: Spring’s dependency injection (DI) and interface-oriented programming model (AOP) and other features can help achieve loose coupling and improve the flexibility and maintainability of the code. sex.

  3. Dependency Injection: The Spring framework supports dependency injection, which manages dependencies between objects through configuration files or annotations, reducing the coupling between components and improving code efficiency. testability.

  4. Aspect-oriented programming: The Spring framework supports aspect-oriented programming (AOP), which can easily implement cross-cutting concerns such as logging, transaction management, and security control, and improve the efficiency of the code. Degree of modularity.

  5. Containerized management: The Spring framework provides an IoC container that can manage the life cycle and configuration of objects, simplifying the creation and management of objects.

  6. Rich functional modules: The Spring framework provides a rich set of functional modules, including data access, transaction management, message queues, security authentication, etc., which can meet the needs of various enterprise-level applications. development needs.

  7. Broad integration: The Spring framework has good integration capabilities with various development frameworks and technologies, and can be integrated with other frameworks (such as Hibernate, MyBatis, Struts, JPA, etc. ) works well together.

  8. Community support: The Spring framework has huge community support and an active developer community, which provides access to rich learning resources and problem solutions.

In general, the Spring framework is highly practical and flexible in enterprise application development and can help developers quickly build reliable applications.
8. Explain the difference between BeanFactory and ApplicationContext in Spring.

BeanFactory is the most basic form of Spring IoC container and provides basic IoC functions. It is lazy initialized, that is, it is created when a Bean is actually used. ApplicationContext is a sub-interface of BeanFactory, providing more enterprise-level functions, such as AOP, event-driven, internationalization, etc. The ApplicationContext is also pre-initialized, that is, all beans have been created when the container starts.

BeanFactory and ApplicationContext are both containers used to manage beans in the Spring framework, but there are some differences between them.
BeanFactory is the most basic container interface of the Spring framework. It provides the most basic IOC container functions and is responsible for instantiating, locating, configuring objects in the application and establishing dependencies between these objects. It uses a lazy loading mechanism, and only when the client requests a bean from the container, the container actually instantiates the bean. The ApplicationContext interface inherits from the BeanFactory interface. In addition to providing all the functions of BeanFactory, it also provides more functions for enterprise-level applications, such as event propagation, internationalization information, resource loading, application layer-specific context, etc. Moreover, ApplicationContext will instantiate all beans at startup, so the startup time may be longer, but subsequent acquisition of beans will be faster. Simply put, BeanFactory provides basic IOC container functions, while ApplicationContext adds more enterprise-level functions on this basis, but it takes relatively more time to start.

9.What are the annotations in Spring?

Commonly used annotations in Spring include:

There are many commonly used annotations in the Spring framework. Here are some common annotations:

  1. @Component: Used to identify a class as a component managed by the Spring container. It is usually used in conjunction with @Autowired for dependency injection.

  2. @Autowired: Used to automatically assemble beans. It can be used on fields, constructors, and Setter methods. Spring will automatically find matching beans for injection.

  3. @Controller, @Service, @Repository: Beans used to identify controllers, services, and data access layers respectively, usually used for more fine-grained component classification .

  4. @Configuration: used to identify the configuration class, usually used together with @Bean to replace traditional XML configuration.

  5. @RequestMapping: Used to map HTTP requests to processing methods. It can be used on classes and methods to specify the mapping relationship between URLs and processing methods.

  6. @Value: Used to inject attribute values ​​and can be used on fields, method parameters, and constructor parameters.

  7. @Transactional: Used to identify methods or classes related to transaction management, so that they have transaction management capabilities.

  8. @Aspect, @Before, @After, @Around: used to implement aspect programming, providing information before, after and after method execution. Handling of surround and other timing.

These annotations greatly simplify the development and configuration of Spring applications and improve the readability and maintainability of the code.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_49841284/article/details/134816093