Spring core overview


usespring, annotation is an inextricable topic.spring-bootCommonly used in@RestControllerAnd@ControllerTo splitapiAndwebdevelopment. Yii is the same as its idea, with controllers in different namespacesyii\rest\ActiveControllerandyii\web\ControllerTo achieve differentiated control. For minimal intrusion,spring-bootDependency injection + dynamic proxy is often used instead of strong reference inheritance. The most typical iswebmvcincontrollerDesign

Core container

Core Containerfromspring-core , spring-beans , spring-context , spring-context-supportandspring-expression(spring expression language) module composition

  • Context ( spring-context )Modules built onCore and BeansOn the solid foundation provided by the module, add support for internationalization (for example, using resource packages), event propagation, resource loading, and transparent creation of context through servlet containers
  • spring-context-supportSupport the integration of common third-party libraries intoSpringIn application context, for caching, mail(JavaMail), scheduling(CommonJ,Quartz)And template engine(FreeMarker,JasperReports,Velocity
  • spring-expression Used to query and manipulate object graphs at run time, named variables, andSpringofIoCRetrieve objects by name, list projection and selection, and common list aggregation in the container

Spring core overview

Annotation processing flow(JSR269(proposal)

  1. The compiler parses the source code to generate an ast abstract syntax tree
  2. Annotation processor stage
  3. Analysis and generation. After the first two steps are completed, bytecode is generated (type erasure)

Annotation processing method

  • Runtime reflection mechanism
  • JavaProvidedAPITo handle compile time annotations

Javac compilation process

  1. Parsing and populating symbol tables
    • Parsing parser lexical analysis syntax analysis
  2. Annotation processing of plug-in annotation processor
    • jdk1. 6 + plug in annotation standard API development
    • The compile time plug-in annotation processor processes annotations
  3. Analysis and bytecode generation

Bytecode enhancement

Java bytecode enhancement refers to modifying Java bytecode after it is generated

  1. Modify bytecode to obtain the original bytecode in memory, and then modify its byte [] array through some tools (such as ASM, javaasist)
  2. Make the modified bytecode effective
    • Method 1: CustomizeClassLoaderTo load the modified bytecode
    • Replace the original bytecode:JVMLoad user’sClassWhen, intercept and return the modified bytecode, or at run time, useInstrumentation.redefineClassesMethod to replace the original bytecode

Java Dynamic Proxy

JDKjava.lang.reflect.ProxyrealizationInvocationHandlerInterface dynamic proxy.
springThe dynamic proxy of uses bytecode enhancement and implements two proxy modes, JDK interface dynamic,cglibInheritance dynamics. The proxy policy is based on whether the target class implements the interface class. If yes, the JDK dynamic proxy is used by default. If cglib is not mandatory, an inherited proxy is used.

  • Dynamic agent coreForward scattered calls to different methods of the object to the same handler function
  • The dynamic agent implements AOP, that is, it can execute its own defined hooks before and after the function is executed
  • Implement RPC
    • PRC essentially disperses function callers and function providers on two different processes
    • Network communication is used for data interaction in the middle. RPC call is actually a proxy for objects on another remote machine process
    • The processing center encapsulates and packages the called information and sends it to another process through the network

JVM class loading steps

  1. Load the binary stream obtained from the fully qualified name of the class
  2. Verify that the information contained in the byte stream of the class file meets the requirements of the virtual machine
  3. Prepare to allocate memory space for class variables and set the initial value of class variables
  4. analysis
  5. Initialization initializes fields and other resources according to the code specified by the user, and executes the static block

    Class.forName(className, true, currentLoader)When the class object is obtained by default, the class will be initialized automatically
    ClassLoader.loadClassThe class is not initialized, so static code blocks in the class are not executed

Meta configuration information

  • A bean definition is essentially a recipe for creating one or more objects
  • Beans and their dependencies are reflected in the configuration metadata used by the container
  • Configure metadata toXML, Java annotation or Java code (Java class) representation

    No matter what form of configuration it is, the container isBeanDefinitionInstance to describe dependencies

Bean dependency management

springProvides a range ofAwareInterface that allows beans to indicate to the container that they need some infrastructure dependency

Interface list and injected dependencies

  • ApplicationContextAwareDeclare application context
  • ApplicationEventPublisherAwareEncapsulate event publisher
  • BeanClassLoaderAwareClass loader for loading bean classes
  • BeanFactoryAwareDeclare beanfactory
  • BeanNameAwareThe name of the declared bean
  • BootstrapContextAwareResource adapter, usually only available in JCA
  • LoadTimeWeaverAwareDefined weavers are used to process class definitions at load time
  • MessageSourceAwareConfigured policy for parsing messages
  • NotificationPublisherAwareSpring JMX notifies Publishers
  • PortletConfigAwareThe portletconfig container is currently running
  • PortletContextAware
  • ResourceLoaderAwareUsed for low-level access to resources
  • ServletConfigAware
  • ServletContextAware

This work adoptsCC agreement, reprint must indicate the author and the link to this article