Day 7: Spring (Basic)

One: Concept

        Spring concept: Make existing technologies more practical. It is a hodgepodge in itself, integrating existing framework technologies

advantage:

        1. Spring is an open source and free framework, container

        2. Spring is a lightweight framework, non-intrusive.

        3. Inversion of control IoC, facet-oriented Aop

        4. Support for things, support for frameworks

        To sum up one sentence: Spring is a lightweight Inversion of Control (IoC) and Aspect-Oriented (AOP) container (framework).

composition:

        1. Core container (basic functions)

        2. Context (configuration file)

        3. AOP (Aspect Oriented)

        4. DAO (database operation)

        5. ORM (object-relational mapping, used to realize the conversion between data of different types of systems in object-oriented programming languages)

        6.Web

        7.MVC

Two: IOC (inversion of control, a design idea)

        Inversion of control is a way to produce or obtain a specific object through a description (XML or annotation) and through a third party. It is the IoC container that implements inversion of control in Spring, and its implementation method is dependency injection (Dependency Injection, DI).

        To put it simply, the power of object creation and the management process of the life cycle of the object are handled by the Spring framework. From then on, in the development process, it is no longer necessary to pay attention to the creation of objects and the management of the life cycle, but by Spring when needed. Provided by the framework, this mechanism by which the Spring framework manages object creation and lifecycle is called inversion of control

benefit:

        1. Centralized management of resources to realize configurable and easy management of resources

        2. Reduced the degree of dependence (coupling) on ​​both sides of resources

The way IOC creates objects:

        1. Create objects using no-argument construction

<bean id="user" class="nuc.ss.pojo.User"> <property name="name" value="狂神"/> </bean>

        2. Create objects using parameterized construction

                1. Subscript assignment

                        <bean id="user" class="nuc.ss.pojo.User"> <constructor-arg index="0" value="狂神说Java"/> </bean>

                2. Type

                        <!--There are 2 constructors, type, but only one of the same type, not recommended--> <bean id="user" class="nuc.ss.pojo.User"> <constructor-arg type= "java.lang.String" value="Mad God"/> </bean>

                3. Parameter name

                        <!--Constructor 3 with parameter name--> <bean id="user" class="nuc.ss.pojo.User"> <constructor-arg name="name" value="Mad God"/ > </bean>

        Summary: When the configuration file is loaded. The objects managed in it have been initialized!

Three: DI (Dependency Injection, a container dynamically injects a dependency into a component, which is a way to achieve IOC)

Method to realize:

       1. Constructor injection:

ditto

        2. Setter injection (emphasis):

Dependency injection:

        Dependency: refers to the creation of the Bean object depends on the container. The dependent resources of the Bean object.

        Injection: Refers to the resources that the Bean object depends on, which is set and assembled by the container.

complex type Address.java

The real test object Student.java

beans.xml

test class

3. Annotation injection

We can use p command space and c command space for injection

use

Note: p naming and c namespace cannot be used directly, and xml constraints need to be introduced!

xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"

Four: Bean

In Spring, those that make up the main body of the application and the objects managed by the Spring IOC container are called beans

1. Scope:

  1. Singleton mode (Spring default mechanism)

<bean id="user" class="nuc.ss.pojo.User" scope="singleton"/>

  1. Prototype mode: Every time you get from the container, a current object will be generated!

<bean id="user" class="nuc.ss.pojo.User" scope="prototype"/>

  1. The rest of request, session, and application can only be used in web development

2. Automatic assembly:

Autowiring is a way for Spring to satisfy bean dependencies

Spring will automatically look for it in the context and automatically assemble properties for the bean!

Specify the autowire mode for a bean definition using the element's autowire attribute

benefit:

Autowiring helps to reduce or even eliminate the configuration of <property> elements and <constructor-arg>, and reduce the number of XML configurations. Of course, we don't need to write XML files, so we are naturally much more relaxed.

Autowiring mode

use:

Autowire by id/name

byName: will be automatically socketed in the container context, and the bean id corresponding to the value behind the set method of its own object

test:

Automatic assembly according to type

Only the autowire parameter is different compared to id/name autowiring

byType: It will be automatically socketed in the container context, and the bean id of the same type as its own object property

Autowiring based on construction parameters

test:

manual designation

test:

Five: Proxy mode

The proxy mode (Proxy) is to access the target object through the proxy object, which can enhance additional functions on the basis of the target object, that is, extend the function of the target object.

1. Static proxy:

interface

real role

agent role

The benefits of static proxy mode:

  • Can make the operation of real characters more pure! No need to pay attention to some public business
  • The public is also handed over to the proxy role! The division of labor has been realized!
  • When the public business expands, it is convenient for centralized management!

shortcoming:

  • A real character generates a proxy character;
  • The amount of code will be doubled - the development efficiency will be lower

2.jdk dynamic proxy:

Features:

1. Proxy objects do not need to implement interfaces

2. The generation of proxy objects is to use the API of JDK to dynamically build proxy objects in memory (we need to specify the type of interface implemented by creating proxy objects/target objects)

3. Dynamic proxy is also called: JDK proxy, interface proxy

interface

real role

Proxy role (proxy class) and client

Benefits of dynamic proxies:

  • Can make the operation of real characters more pure! No need to pay attention to some public business
  • The public is also handed over to the proxy role! The division of labor has been realized!
  • When the public business expands, it is convenient for centralized management!
  • A dynamic proxy class proxies an interface, which generally corresponds to a type of business
  • A dynamic proxy class can proxy multiple classes, as long as the same interface is implemented [Core]

3. Cglib dynamic proxy:

Cglib proxy, also called subclass proxy, builds a subclass object in memory to extend the function of the target object.

Proxy role (proxy class) and client

Three characteristics:

1. Static proxy, the object to be proxied must be written in the class, only one class can be processed, the execution efficiency is high, the code is highly coupled, and the reusability is poor.

2. JDK dynamic proxy means that the proxy class must implement the InvocationHandler interface. There is a (method.invoke(object, parameter) method in the interface, which uses reflection to execute the method of the proxy object; Java dynamic proxy through the Proxy.newProxyInstance() method Dynamically obtain the proxy object, this method has three parameters: (class loader, interface, subclass instance of the InvocationHandler interface); one of the parameters is the interface, that is to say, the Java dynamic proxy can only proxy the class that implements the interface, If the proxied class does not implement any interface, it cannot implement JDK dynamic proxy.

3. Cglib dynamic proxy is different from JDK dynamic proxy through interface. Cglib dynamic proxy is realized through inheritance, by generating subclass bytecode, rewriting the method of the proxy class, and enhancing the function in the rewritten method; because Cglib dynamic The proxy should inherit the proxied class, so the class or method modified by final cannot implement Cglib dynamic proxy.

Six: AOP

1. AOP: aspect-oriented programming, is a programming idea

2. Overview:

1. Cross-cutting concerns

Which methods to intercept and how to deal with them after interception, these concerns are called cross-cutting concerns

2. Aspect

A class is an abstraction of object characteristics, and an aspect is an abstraction of cross-cutting concerns

3. Joinpoint

The intercepted point, because Spring only supports method-type connection points, so in Spring, the connection point refers to the intercepted method. In fact, the connection point can also be a field or a constructor

4. Pointcut

Definition of interception on join points

5. Notification (advice)

The so-called notification refers to the code to be executed after intercepting the connection point. Notifications are divided into five categories: pre-, post-, exception, final, and surround notifications.

6. Target audience

proxy target

7. Weave

The process of applying an aspect to a target object and resulting in the creation of a proxy object

8. Introduction

Without modifying the code, the introduction can dynamically add some methods or fields to the class at runtime

4. Implementation method

Method 1: Use Spring's API interface [main Spring API interface implementation]

First write our business interface and implementation class

Write our enhancement class, we write two, a pre-enhancement and a post-enhancement

Register in the spring file, and implement aop cut-in implementation, pay attention to import constraints.

test

Method 2: Customize to achieve

The target business class is still userServiceImpl

Write a cut-in class of our own

Go to spring to configure

test

Method 3: Use annotations to achieve!

Write an enhanced class for annotation implementation

In the Spring configuration file, register the bean and add configurations that support annotations

benefit:

Decoupling: achieve low coupling and high cohesion

Realize code reuse and improve usage efficiency

Seven: Affairs

Generally speaking, a transaction is a group of atomic operation units. From the database point of view, it is a group of SQL instructions. Either all of them are executed successfully. If there is an error in the execution of one of the instructions for some reason, all the previously executed instructions will be revoked. The simpler answer is: either all executions are successful, or cancellations are not executed.

Properties of things (ACID):

Atomicity: the smallest unit of things, no division is allowed

Consistency: before and after the execution of the transaction, the data is kept consistent

Isolation: When accessing the database concurrently, the user's transaction will not be interfered by other things, and the concurrent databases are independent

Durability: After the transaction is committed, it will not be affected in any way

transaction type:

1. JDBC transaction

2. JTA transaction

3. Container transactions

Guess you like

Origin blog.csdn.net/qq_35056891/article/details/126652511
Recommended