Spring Framework - Core Concepts of Interview Questions

Table of contents

1. What is the role of the Spring framework?

2. What is DI?

3. What is AOP?

4. Spring common annotations

5. Design Patterns in Spring

6. The scope of several beans supported by Spring

7. What is the life cycle of Bean in Spring?

8. Transaction management in Spring

9. There are several ways of dependency injection in Spring

10. What are the configuration methods of Spring


1. What is the role of the Spring framework?

Main points:

Spring is an open source framework, mainly to simplify the development of enterprise-level applications. It is an IOC and AOP container framework. The core functions are as follows:

(1) DI depends on the injection container to realize interface-oriented and modular development.

(2) AOP is oriented to the aspect container to realize the dynamic unified enhancement of the program.

(3) Provide Session management and declarative transaction management for different ORM technologies to simplify data layer development.

(4) Provide an excellent SpringMVC framework, simplify the development of the interface layer, and easily integrate other MVC frameworks.

(5) Provide Spring Boot quick configuration and Spring Cloud microservice architecture. Simplify the implementation of various enterprise-level technologies: such as NoSQL, big data, distributed technologies, etc.

2. What is DI?

Main points:

DI (Dependency Injection) refers to dependency injection or inversion of control (IOC). It means that the sub-objects on which objects in the program depend are not created in hard coding, but are injected at runtime through containers (such as Spring).

    DI makes the modules only depend on the interface, not the implementation class (the implementation class is injected by the container at runtime), so that software modules can be developed and tested independently, reducing the degree of coupling.

3. What is AOP?

main points:

AOP (Aspect Oriented Programming) aspect-oriented programming. A way to inject enhanced code into a method without modifying the original code. AOP weaves external code into the original program through proxy mode or dynamic proxy. Typical application scenarios of AOP are transactions, logs, permissions, exception handling, etc.

AOP uses the "cross-cutting" technology to dissect the inside of the encapsulated object, and encapsulates the public behavior that affects multiple classes into a reusable module, and names it "Aspect", which is the aspect. The so-called "aspect" simply means that the logic or responsibility that is not related to the business but is commonly called by the business modules is encapsulated, which is convenient to reduce the duplication of code in the system, reduce the coupling between modules, and is conducive to future operability and maintainability.

4. Spring common annotations

Main points:

(1) Component related:

@Component components, such as utility classes

@Service is used in the business logic layer

@Repository is used in the data access layer

@Controller controller declaration

(2) Dependency injection:

@Autowired: provided by Spring, implements dependency injection by type

@Resource: provided by JSR-250

(3) Configuration notes:

@Configuration declares the current class as a configuration class

The @Bean annotation is on the method, declaring that the return value of the current method is a bean, replacing <bean/>

@ComponentScan sets the scan scope of the package

(4) AOP annotations:

@Aspect declares an aspect (on a class)

@After post notification

@Before pre-advice

@Around around notifications

@PointCut declares a point cut

@Scope sets the scope of the Bean

5. Design Patterns in Spring

Main points:

    Proxy mode: use dynamic proxy in AOP to realize aspect programming

    Singleton mode: The bean defined in the spring configuration file defaults to the singleton mode.

    Template method pattern: used to solve the problem of code duplication.

    Front controller mode: Spring provides DispatcherServlet to distribute requests.

    Dependency injection mode: runs through the core concept of the BeanFactory / ApplicationContext interface.

    Factory mode: BeanFactory is used to create instances of objects.

6. The scope of several beans supported by Spring

Main points:

     singleton: singleton, default scope, only one instance exists in the SpringIOC container

     prototype: Every time the bean defined by the prototype is obtained through the Spring container, a new Bean instance will be created, and each Bean instance has its own properties and state.

     request: In an Http request, the container will return the same instance of the Bean. A new bean will be generated for different Http requests, and the bean is only valid within the current Http Request.

     session: In an Http Session, the container will return the same instance of the Bean. A new instance will be created for different Session requests, and this bean instance is only valid within the current Session.

      global Session: In a global Http Session, the container will return the same instance of the Bean, which is only valid when using the portlet context.

7. What is the life cycle of Bean in Spring?

main points:

Bean's life cycle has gone through a series of methods, and the key processes are as follows:

(1) Instantiate a Bean, which is commonly referred to as new;

(2) Initialization: Configure the instantiated Bean according to the Spring context, that is, IOC injection. If the init-method attribute is configured in the Spring configuration of this Bean, its configured initialization method will be called automatically

(3) Bean call: get the bean and call it;

(4) Bean destruction: When the Bean is no longer needed, it will go through the cleaning stage. If the Bean implements the DisposableBean interface, the destroy method implemented by it will be called. If the destroy-method property is configured in the Bean's Spring configuration, its configured destruction method will be called automatically.

8. Transaction management in Spring

Main points:

As an enterprise application framework, Spring defines an abstraction layer on top of different transaction management APIs. Application developers do not need to understand the underlying transaction management API to use Spring's transaction management mechanism. Spring supports both programmatic transaction management (also known as coded transaction) and declarative transaction management.

Programmatic transaction management: Embed transaction management code into business methods to control the commit and rollback of transactions. In programmatic transactions, additional transaction management code must be included in each business operation.

    Declarative transaction management: In most cases, it is better to use than programmatic transaction management. It separates transaction management code from business methods and implements transaction management in a declarative manner. Transaction management, as a cross-cutting concern, can be modularized through the AOP approach. Spring supports declarative transaction management through the Spring AOP framework.

9. There are several ways of dependency injection in Spring

Main points:

(1) set attribute injection

(2) Constructor injection

(3) Static factory method injection

(4) Instance factory method injection

10. What are the configuration methods of Spring

Main points:

   Spring supports xml configuration, annotation configuration and JavaConfig configuration in three forms.

(1) Based on xml configuration

<bean id=”dao” class=”com.demo.dao.impl.UserDaoImpl”></bean>

(2) Annotation-based configuration

Instead of using XML to describe bean wiring, beans can be configured as component classes themselves by using annotations on the relevant class, method, or field declarations. By default, annotation wiring is not turned on in the Spring container

<beans>
    <context:annotation-config/>
</beans>

(3) Configuration based on Java API

Spring's Java configuration is achieved by using @Bean and @Configuration.

The @Bean annotation plays the same role as the <bean /> element.

@Configuration marks the configuration class, replacing the configuration file in the xml configuration

Guess you like

Origin blog.csdn.net/qq_55917018/article/details/131129242