Spring

1. Basic Concepts

1. Advantages of Spring

1. Lightweight, non-invasive framework; Has no impact on the existing class structure 2, can provide many services, such as transaction management, WS(WebService), etc. 3, AOP is very good support, convenient for section programming, business logic and system services separate 4, provides a good integration support for the mainstream framework, Such as Hibernate, Struts2, JPA, etc., like a glue, glue some good framework together to facilitate the use of 5, the use of Spring IOC container, the dependency between objects to Spring, reduce the coupling between components, 6. The Spring DI mechanism reduces the complexity of business object substitution. 7. Spring is highly open and does not force you to rely on Spring. Developers are free to choose part or all of SpringCopy the code

2. Disadvantages of Spring

1, lack of a common controller 2, no SpringBoot easy to use 3, Spring is like a glue, stick the framework together, it is not easy to splitCopy the code

3. What is DI

Dependecy Injection and Inversion of Control are the same concept. In traditional programming, when a role requires the assistance of another role, it is usually the caller who creates the instance of the called. But the job of creating the called in Spring is no longer done by the caller, so it's called inversion of control. Spring does the job of creating the called, and then injecting the caller, which is also called dependency injectionCopy the code

Second, the Aop

1. Basic concepts

Develop the core and aspect functions separately, and then "weave" the aspects and core business functions together, which is called AOP. 2. Separate the concern code from the business code (good isolation between steps, source code independence). Repetitive code extraction for many functions and dynamic implementation of "facet class code" on business methods at run time. 4, application scenarios: log, transaction management, permission controlCopy the code

2. Implementation principle

  1. JDK dynamic proxy

It is implemented through proxy.newProxyInstance () and InvocationHandler

  1. cglib

    The generated object type is Enhancer

    The implementation is similar to JDK dynamic proxies, except that the proxy objects generated at run time are subclasses of the target class extension

  2. Static agent

    The proxy classes are generated directly at compile time

    Disadvantages: If you want to delegate multiple implementations of an interface, you need to define different proxy classes. The proxy class and the proxy-class must implement the same interface. In case the interface changes, both the proxy and the proxy-class must be modified

  3. JDK vs. Cglib

3. Configuration mode

  1. XML way

  2. Annotation way

  3. JAVA class-based configuration

    This is done with @Configuration and @bean annotations

    @Configuration works on the class as an XML Configuration file;

    @beans act on methods and are equivalent to beans in XML configuration;

Third, the IOC

1. Dependency injection

  1. Assembly mode (specific behavior of dependency injection)

    I. Annotation-based autoassembly

    Comments:

    @Autowired(byType)+@Qualifier

    @Resource(byName)

    Automatic scanning:

    component-scan

    Ii. Explicit assembly based on XML configuration

    Iii. Explicit assembly based on Java configuration

    Errors can be found at compile time

  2. Dependency injection

    I. Constructor injection

    Constructor dependency injection is implemented by the container firing the constructor of a class that has a series of parameters, each representing a dependency on another class.

    For example: <bean id="hello" class="com.maven. hello" ><property name="text" ref="text" /></bean>Copy the code

    Ii. Settter method injection

    It is called setter method injection because it is done by finding the corresponding setter method for a class and then injecting it accordingly

    Setter-based dependency injection is implemented when the container calls Setter methods of the bean after it has instantiated the bean by calling either a no-argument constructor or a no-argument static factory method.

    Iii. The best solution is

    Mandatory dependencies are implemented with constructor arguments and optional dependencies are implemented with setter methods

  3. The loop is dependent on I, and the constructor approach cannot solve this problem, except to throw an exception

    Ii. Multiple cases can not be solved, but can only throw an exception

    Because the Spring container does not cache “prototype” scoped beans, there is no way to expose a bean being created in advance.

    Iii. Singleton mode can be solved

    Expose a bean being created by exposing a singleton factory method ahead of time so that other beans can reference that bean

    Iiii, instead of creating an object, just describe how it is created, do not assemble components and services directly in code, but describe which components require which services in a configuration file, and then a container (IOC container) takes care of assembling them.

2. Container initialization process

1. The Resource location; The process of locating resources on a BeanDefinition. In layman’s terms, you find the XML file that defines the Javabean information and encapsulate it as a Resource object.

2. Loading of BeanDefinition; The user-defined Javabeans are represented as data structures inside the IoC container, which are known as BeanDefinitions.

3. Register these BeanDefinitions with the IoC container.

3. Bean knowledge

  1. The life cycle of the bean

2. Scope of the bean

1). singleton

Singleton Bean is the default scope by default is the time to create container initialization, but also can be set runtime to initialize the Bean DefaultSingletonBeanRegistry class singletonObjects hash table to save the singleton. There is only one shared Bean instance in the Spring IoC container, always pointing to the same object no matter how many beans reference it. The Spring container can manage the life cycle of beans under the Singleton scope, where Spring knows exactly when a bean is created, initialized, and destroyedCopy the code

2). prototype

Each time a Prototype-defined bean is fetched from the Spring container, the container creates a new bean instance. Each bean instance has its own properties and state. The Spring container will no longer track its life cycle and will not manage the life cycle of beans that have been configured to prototype scope. Use the Prototype scope for stateful beans and the Singleton scope for stateless beans.Copy the code

3). request

In an Http request, the container returns the same instance of the Bean. A new Bean is generated for different Http requests and is valid only within the current Http Request.Copy the code

4). session

In an Http Session, the container returns the same instance of the Bean. New instances are created for different Session requests, and the bean instance is valid only for the current Session.Copy the code

5). global Session

In a global Http Session, the container returns the same instance of the Bean, valid only if the portlet Context is used.Copy the code
  1. The creation of a bean

    1. Enter the getBean() method. 2. Check whether the current bean's scope is a singleton. If it is not a singleton, create createBeanInstance 3. After the new instance is created, the property (populateBean) will be injected and callback 1 will be handled. Create bean 2). Find @autoWired object 3). Create injection object and assign valueCopy the code

4. General process

Fourth, business management

1. Basic concepts

If a set of operations is required to be atomic, the transaction is annotated to start and the commit or rollback operation is performed according to the given transaction rules

Transaction management is generally at the Service layer

2. Transaction control

Programmatic transaction control (the user manually controls transactions in code)

1).Conn.setAutoCommite(false); 2). Fine-grained, flexible, but tedious to develop: each time to open, commit, rollbackCopy the code

Declarative transaction Control (Spring provides control management of things)

1).xml mode 2). Annotation modeCopy the code

3. Transaction properties

  1. Transaction propagation behavior

    Required_new: If the current method has a transaction, the current method transaction is suspended, and a new transaction is started for the added method until the new transaction is completed and the current method’s transaction starts

    Required (default method): Joins the current method transaction if the current method already has a transaction

    The other five ways (Extended Learning)

    Transaction propagation behavior is used to describe how a transaction propagates when a method modified by one transaction propagation behavior is nested into another method.

  2. Isolation levels for databases (see the isolation levels article)

  3. Transaction timeout attribute

    Refers to the maximum time that a transaction is allowed to execute and automatically roll back the transaction if it has not completed.

  4. Transaction read-only attribute

    Whether a transaction resource is read-only

  5. Rollback rules

Defines which exceptions cause a transaction to roll back and which do not. By default, transactions are rolled back only when they encounter run-time exceptions, not when they encounter checker exceptions, and can be user-defined

4. Spring Transaction Management Interface

PlatformTransactionManager

TransactionDefinition

TransactionStatus

5. Transaction management is generally at the Service layer

In the DAO layer, only the current method can be rolled back, but generally our service layer methods are made up of many DAO layer methods

If you are at the DAO layer, the number of commits is excessive

Fifth, for SpringMVC

1. Execution process

2, annotations,

3. Servlet lifecycle

1. Load and instantiate

2. The initialization

3. Request processing

4. Termination of services