Spring interview knowledge Lite

table of Contents

 

Spring

What is Spring

Spring MVC flow

To resolve circular dependencies

Bean's life cycle

Bean scopes

IOC (DI)

Spring AOP


Spring

What is Spring

Spring is a collection contains a series of functions, such as the rapid development of Spring Boot, support micro-services Spring Cloud, supports authentication and authorization of Spring Security, Web framework Spring MVC. IOC and AOP remains at the core.

Spring MVC flow

  1. Transmission request -> DispatcherServlet interceptor to get HandlerMapping
  2. In turn call interceptor configuration, and finally found the configured business code execution Handler and business methods
  3. Packaged ModelAndView returned to render the page parser ViewResolver

To resolve circular dependencies

No argument constructor, field injection

Bean's life cycle

  1. Spring Bean to instantiate
  2. Spring Bean reference value and injected into the corresponding attribute Bean
  3. The container through the interface to the container information Aware injection Bean
  4. BeanPostProcessor. For further construction, will perform the corresponding method is currently being initialized bean object will be passed in before and after InitialzationBean, we can make any deal with the bean
  5. InitializingBean. This stage can also increase the bean before the formal structure complete our custom logic, but it is different from the pre-processing, since this function will not be passed in the current bean object, and therefore no way to deal with the object itself at this point, only You can add some additional logic.
  6. DisposableBean. Bean will always reside in the application context to use application until the application context is destroyed, if the Bean implements the interface, Spring will call its destory method

Bean scopes

  • singleton: Singleton, Spring IoC container, there is only one shared instance of Bean, Bean no matter how many refer to it, always point to the same object.
  • prototype: When the prototype model, every acquisition prototype defined by the Spring bean container, the container will create a new instance of the Bean, Bean each instance has its own properties and status.
  • request: In a Http request, the container is returned to the same instance of the Bean. And different Http request will generate a new Bean, and the bean is only valid in the current Http Request.
  • session: In a Http Session, the container will return the same instance of the Bean. The different Session request a new instance is created, the bean instance is only valid within the current Session.
  • global Session: a global Http Session, the container will return the same instance of the Bean, the only effective when portlet context.

IOC (DI)

  • Inversion of Control

Spring IOC container is responsible for the relationship between the life cycle and the target object. IoC container control to create an object, dependent objects are inverted to obtain a
no IoC when we all take the initiative to create the dependent objects in their own object, which is forward. However, with the IoC, injection directly dependent objects created by the rear IoC container to the subject to be injected, depending actively obtain the original object into the passive acceptance, it is an inverted

  • Dependency Injection

Dependencies between components at runtime is determined by the container, the container will inject a dynamic dependencies among components to enhance component reuse frequency, flexible, extensible
dependency injection mechanism, we need only a simple configuration, without any code can specify the target resources needed to complete their business logic without having to be concerned about the specific resources come from, who realized

Injection mode: constructor injection, the setter method of injection, injection Interface

Spring AOP

Introduction

Aspect-oriented programming, is a programming technique that is OOP (Object Oriented Programming) added and improved. Execution of OOP is a top-down process, and there is no relationship left to right. Therefore, OOP programming, there will be a large number of duplicate code. AOP sucked and service independent of these duplicate code extracted, and then embedded into the service code them. Common applications include: rights management, logging, transaction management.

Method to realize

AOP implementation technology, divided into two categories: one is dynamic agent technology, the use of the embodiment intercepted message, the message decorated, instead of performing the original object behavior; second static weaving manner, the introduction of a specific syntax to create "aspect", so that the compiler can be woven into the code related to "aspect" during compilation. Spring AOP is implemented in a dynamic proxy.

Spring AOP proxy dynamic principle used

jdk reflection: reflection generated by the proxy class bytecode files, calls to specific pre-processed method invocation InvokeHandler
cglib tool: asm using open source packages, class files proxy object classes loaded in, by modifying bytecode subclassing to deal with

  1. If the target object implements the interface, it will use JDK dynamic proxy AOP implementations default
  2. If the target object implements the interface, you can force the use of CGLIB achieve AOP
  3. If the target object does not implement the interfaces must be used CGLIB library, spring will automatically between JDK dynamic proxies and convert CGLIB
Published 142 original articles · won praise 31 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_38905818/article/details/103803106