Spring characteristics and working principle

The plurality of reference, are summarized as follows:

Spring concept

  J2EE application framework Spring is a multi-layered, its core is to provide a new mechanism for managing business objects and their dependencies. It is a container frame, the bean is used to create, maintain a relationship between the bean, which can manage web layer, a durable layer, the business layer and the like, the components may be configured to maintain the relationship of the individual layers and the individual layers

Spring Features

  (1) Lightweight: Lightweight is for heavyweight container (EJB), it's, Spring core package not to 1M size, and resources required to use Spring's core package is also very small, so you can in a small used in the device.  

  (2) non-invasive: Spring target is a non-invasive service framework. The reason is that all frameworks are providing a lot of functionality for users to use, thereby simplifying development time and costs, but due to extensive use of the API framework, the framework of a large number of applications and dependencies happened, can not be independent from the frame , can not make the program more components for use in other programs.

  (3) container: Spring provides container function, the container can manage the life cycle of objects, relations between objects and object, we can set the object relationships and the initial value by writing XML, so the container after the start, all objects are directly It can be used without having to write any code to produce an object. Spring There are two different containers: Bean factories and application context

Spring Works

  The core of the interior Spring is the IOC, and the dynamic injection, so create an object not new, and can be automated production, which in fact is the use of java in the reflection, reflection is actually a dynamic to create at run time, the calling object, Spring that is, at run time, with xml Spring configuration file to dynamically create objects and objects in the method call.

  There is a core Spring AOP Aspect Oriented Programming can be monitored and controlled as a type of object (that is, before and after the specific method calls to call this type of object modules you specify) so as to achieve a functional expansion module. These are achieved by configuring class.

  Spring object : the object is to make the relationship with the object (modules and the module) between not be related by the code, are described by the configuration management class (Spring assembly to dynamically reflected by the object according to the internal configuration)  

     Remember: Spring is a container, all objects in the container will have these services and functions provided by Spring.  

     Spring in the most classic of a design pattern is used: Template Method pattern.      

spring's two core technologies:

ioc / di: Inversion of Control / DI

  [ Core interior is the IOC, and the dynamic injection, so create an object not new, and can be automated production, which in fact is the use of java in the reflection, reflection is actually a dynamic to create at run time, the calling object, Spring that is, at run time, with xml Spring configuration file to dynamically create an object, and the object inside the method call. ]

  The so-called IoC ( Inversion of Control ) , the framework for the spring, it is from the spring to be responsible for the life cycle of the control object and relationships between objects .

  A focus of IoC ( DI  Dependency Injection ) system is in operation, the dynamic of an object to provide what it needs other objects (and therefore also known as DI) . A reference object is defined as a B (object), the object B as to how this structure, when the configuration, A does not need to know. The system is running, spring at the appropriate time will create a B object, and then the same as injections, which is injected into A, thus completing the control of the relationships between the various objects. A B need to rely on to work properly, and the B to A is injected by the spring of dependency injection is the name came about. DI Principle: Java is an important feature of 1.3 after reflection (Reflection), when the program is running that allows the generation of dynamic objects, the object execution method, changing object properties, Spring injection is achieved by reflection .

  Without spring: in an object, if you want to use another object, you have to get it (himself a new, or a query from JNDI), even after you are finished using the object is destroyed (such as Connection, etc.), subject always and other interfaces or class of coupling up.

  When spring: All classes will be registered in the spring container, tell spring you are something, you need something, then spring will be the appropriate time, the things you want to take the initiative to give you the system is running, but also to you other things you need to. All of the class is created, destroyed by the spring to control, that control object is no longer referenced its object life cycle, but the spring. For a specific subject, before it is to control other objects, all objects are now controlled spring, so called inversion of control.

aop: aspect-oriented:

  AOP (Aspect-OrientedProgramming, aspect-oriented programming), can be said to OOP (Object-Oriented Programing, object-oriented programming) of supplement and perfect .

  AOP's core idea is "the application business logic with its support universal service were separated ."

  AOP the software system is divided into two parts: core concerns and crosscutting concerns . The main flow of business processes is a core concern, not part of the relationship is with crosscutting concerns. It features a crosscutting concern is that they are often accompanied by the use of before and after the core concern, and everywhere similar. Such as certification authority, logging, transaction processing.

  AOP technology, called use "cross" technique, the inside cross-sectional unlock the object of the package, and will affect the behavior of a plurality of classes that the common package into a reusable module, and entitled "Aspect", simply put, that is, those independent of the business, but is the responsibility of the business logic or common module called encapsulated, easy to reduce duplication of code system, to reduce the coupling between modules , and facilitate future operability and maintainability sex. AOP implementation technology, mainly divided into two categories: one using the dynamic agent technology, the use of the embodiment intercepted message, the message decorated, instead of performing the original object behavior; Second, using 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 dynamic proxy weaving, while the weaving AspectJ using compiler and class loading of weaving.

  Spring AOP uses two kinds of dynamic proxy mechanism : one is based on the JDK dynamic proxy , one is based on a dynamic agent of CGLib . After JDK1.3, java provides a dynamic agent technology, allows developers to create dynamic interface agent instance during operation, dynamic proxy JDK classes are mainly related to two java.lang.reflect package: Proxy and InvocationHandler. CGLib with very bottom of the byte code technology, subclass a class, and use the end of the process to intercept calls to intercept all the parent class method in the subclass, and homeopathic weaving transverse logic.

spring basic usage:

1, spring on the container:
  spring containers is the core of the Spring, the container manages the java spring assembly,
  the ApplicationContext the ClassPathXmlApplicationContext new new CTX = ( "bean.xml"); // this instantiate the container, the container will be automatically pre-initialization All Bean instance
  ctx.getBean ( "beanName");
  ApplicationContext instance is the Spring container.
  ApplicationContext container instantiates all default Bean Singleton
  the Spring container management component is not mandatory standard javabean.

2, Spring core mechanisms: dependency injection.
  Whether dependency injection (Dependency Injection) or inversion of control (Inversion of Conctrol) , exactly the same meaning:
  when a java example (caller) need to call another java example (the callee), the conventional case, by caller to create an instance of the caller, typically created by new new,
  created by the caller dependency injection mode will not work done by the caller, so called " inversion of control "; Create is called examples of those usually work done by Spring, and then injected into the caller, so it is called " dependency injection ."

3, there are two ways dependency injection:
  Set injection: IoC containers using injection property setter instance is dependent manner. <property name = "" ref = "">
  configuration injection : Constructor used to inject the dependent instance IoC container. <constructor-arg ref = ""
>   arrangement structure when implanted <constructor-arg> may be configured to index property, used for specifying the number of configuration parameter values as the first configuration parameter value. Subscript starts at 0.

 

Reference (very comprehensive): https://blog.csdn.net/y13530828499/article/details/6648155

Guess you like

Origin www.cnblogs.com/116970u/p/11441658.html