Knowledge points of Spring's interview frequently asked

Spirng framework is very important, it is a basis for us to learn other frameworks, Spring has many important knowledge points, such as IOC, DI, AOP, etc., Spring related knowledge is also a module frequently asked by interviewers, so we have to To understand some of the underlying implementation principles of Spring, the following summarizes some of the questions that Spring is often asked in interviews:

1. The scope of Spring Bean
①singleton: singleton scope, by default, singleton mode is used to create beans
②prototype: prototype scope, prototype mode is used to create beans
③request: the same Http Request shares a Bean object, and different Requests own Different Bean objects
④session: the same Http Session shares a Bean object, and different sessions have different Bean objects
⑤application: the global Web scope, similar to the Application in Servlet

2. Spring Bean
registration method
①XML configuration file registration method ②Java annotation registration method (@Component annotation and @Bean annotation)
③Java API registration method

3. Problems
with beans with the same name. The id and name of the bean in the same Spring configuration file cannot be repeated, otherwise an error will be reported when the Spring container starts. But if Spring loads multiple configuration files, the problem of beans with the same name may occur. Bean with the same name refers to multiple beans with the same name or id. Spring's processing rule for beans with the same name is to use the last Bean to cover the previous Bean, so when we define the Bean, we try to use long-named non-repetitive definitions to avoid the problem of beans with the same name. It is not necessary to specify the id or name attribute of the bean. If you leave it blank, the container will automatically generate a unique name for the bean, so that there will be no problem with the bean with the same name.

4. Spring IOC
IOC-Inversion of Control, or inversion of control, is not a technology, but a design idea. IOC allows programmers not to pay attention to how to create objects, but to focus on operations after object creation, and delegate the creation, initialization, and destruction of objects to the spring container.

5. Spring IOC three ways to create objects ①Using the
default construction method ②Using
static factory method ③Using
instance factory method

For details, please refer to the article: https://www.cnblogs.com/ysocean/p/7466217.html#_label2

6. When DI dependency injection
creates an object, it may depend on other objects, that is, how to assign values ​​to the attributes of the class, and the knowledge points of DI are used. In simple terms, dependency injection is to assign values ​​to attributes (including basic data types and references). data type), dependency injection through the bottom reflection achieved

Two ways of dependency injection:

  • Use constructor injection
  • Use Setter method to inject

For details, please refer to the article: https://www.cnblogs.com/ysocean/p/7471451.html

7. Timing of creating objects in the
Spring container ①By default, when the Spring container starts, it will create the object (read the Spring configuration file, and create it when it encounters a bean)
②You can configure a property lazy-init=“true” for the bean , After configuring this property, Spring will create the object in context.getBean

There are advantages and disadvantages to the two timings of creating objects:

  • In the first case, you can check the correctness of the spring container configuration file when starting the spring container. If combined with tomcat, if the spring container cannot start normally, the entire tomcat cannot start normally. But the disadvantage of this is that some beans are placed in memory prematurely. If there is data, it will consume memory.
  • In the second case, memory consumption can be reduced, but it is not easy to find errors

8. The life cycle of Spring Bean ① Bean
instantiation: It can be seen from the foregoing that the creation time of the bean object may be when the Spring container is started or when the getBean method is called. When the bean object is created, the instantiation of the Bean will be triggered In fact, it can also be understood as a new object, which executes the construction method of the class; it should be noted that because the scope of the bean may be the default singleton singleton scope, the bean will only be triggered when the getBean method is called for the first time After the instantiation of
Bean , the next step is to directly return the existing bean object; ②Call the Bean initialization method initializeBean()
③After the initialization is completed, the Bean object can be used normally, and the method of the Bean object itself can be called.
④The destruction of the Bean object : The destruction method of the Bean object will be executed when the Spring container is closed, but the Spring container will not automatically call the destruction method, but we need to call it actively.

Note: There are two types of containers in Spring, one is the BeanFactory container, and the other is the ApplicationContext container; the methods of destroying the Bean called by the two containers are different

9. Spring AOP
AOP (Aspect Oriented Programming), usually referred to as aspect-oriented programming, the realization of AOP is based on dynamic proxy implementation, it extracts common code modules, when you need to use this common code to enhance business logic Use a "cross-cutting" technology to enhance, do not modify to the original code, to meet the ocp principle; the advantage of Spring AOP is that it can reduce repetitive code, improve development efficiency, and enable business modules to concentrate on business-related code. Reduce coupling

For details, please refer to the article: https://www.cnblogs.com/ysocean/p/7476379.html

Supplement: Spring's three core components: Core, Context, Bean

Guess you like

Origin blog.csdn.net/can_chen/article/details/107850507