Spring (continuous update)

Posted by Garath531 on Thu, 11 Nov 2021 06:06:42 +0100

Spring

xml configuration of spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
	 	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

</beans>

IOC

IoC Inversion of Control is a theory and guiding ideology. Guide developers how to use and manage objects. The creation of objects, attribute assignment, and object life cycle are all managed by containers outside the code.

Control and inversion

  • Control: object creation, attribute assignment, object declaration cycle management

  • Inversion: the developer's permission to manage objects is transferred to the container implementation outside the code, and the container completes the creation of objects.

  • Forward conversion: developers use the new construction method to create objects in the code. Developers have mastered the whole process of object creation, attribute assignment and object destruction. The developer has full control over the object.

Can I use the objects in the container through the container (the container has created the object, assigned the object attribute, and assembled the object)

Spring is a container that can manage objects, create objects, and assign values to attributes

Technical implementation of IoC

DI (Dependency Injection): Dependency Injection, abbreviated as DI, is a technical implementation of IoC. The program only needs to provide the name of the object to be used. How to create the object, how to find it from the container and how to obtain it are implemented by the container itself.

Dependent noun: for example, ClassA class uses the attributes or methods of ClassB, which is called ClassA dependent on ClassB,

public class ClassB{

     public void createOrder(){}

}

public class ClassA{

     //attribute
     private ClassB b = new ClassB();
     
     public vouid buy(){
     
        b.createOrder();
        
     
     }

}

implement ClassA of buy()

ClassA a = new ClassA();
a.buy();

The Spring framework uses DI to implement IoC

Through the spring framework, you only need to provide the used object noun to obtain the object corresponding to the name from the container.

The reflection mechanism used by the bottom layer of spring creates objects and attributes through reflection.

Meaning of spring package

Spring core package: spring core

unit testing

//Create factory object
public void test(){
	//Read the factory information of the configuration file information inside the project
	ApplicationContext c = new ClassPathXmlApplicationContext("appcationContext");
	UserDao ud=(UserDao) c.getBean("userDao");
	ud.add();
}

DI

Dependency injection requires IOC environment configuration

Injection mode

  • set
  • constructor
  • annotation

constructor

A parameterized constructor with corresponding injection parameters is required

<bean id="user" class="cn.hp.entity.User">
	<constructor-arg name="name" value="petty thief"></constructor-arg>
	<constructor-arg name="age" value="98"></constructor-arg>
</bean>

SET

A parameterless constructor is required

<bean id="user" class="cn.hp.entity.User">
	<property name="name" value="petty thief"></property>
	<property name="age" value="98"></property>
</bean>

Annotation injection

The annotation needs to be opened, otherwise it cannot be recognized

  • @Service: bean used to control the service layer

  • @Controller: bean used to control the registration control layer

  • @Repository: bean used to register dao layer

  • @Component: can be used to register all bean s

//Open annotation
<context:component-scan base-package="cn"></context:component-scan>

@Autowired+@Quelifier≈@Resource

Autowired injects by type. By default, there must be a type to be injected. If it is allowed not to be null, it can be combined with the configuration. required is false

By assembling the @ Qualifier annotation with the name of the specific spring bean we want to use, the Spring framework can find what we want from multiple beans of the same type that meet the assembly requirements

Resource is injected by name. The default is the name of the current type. You can also define name = ""

AOP aspect oriented programming

AOP is the continuation of OOP, a hot spot of software development, an important content of Spring framework, and a derived generic of functional programming.

AOP can isolate each part of business logic, reduce the coupling between each part of business logic, improve the reusability of program, and improve the efficiency of development.

Classic applications: transaction management, performance monitoring, security checking, caching, logging, etc

String AOP is implemented in pure Java and does not require a special compilation process and class loader. During runtime, enhanced code is woven into the target class through proxy.

Aspect | is an AOP framework based on Java language. Since spring 2.0, Spring AOP has introduced support for aspect. Aspect expands the Java language and provides a special compiler to weave the code during recompilation.

Related terms in AOP development

In order to learn Aspect and complete AOP development, you must understand the meaning of AOP related terms.

  • JoinPoint: the so-called connection point refers to those intercepted points, and the connection point refers to the methods in the class.
  • Pointcut: refers to the "connection points" to be intercepted. The actually enhanced method is called the pointcut.
  • Advice: refers to the things to be done after intercepting the "connection point" (i.e. enhanced code). Notifications are divided into pre notification, post notification, surround notification, final notification and exception notification.
  • Introduction: it is a kind of special notification, which acts at the class level (understanding)
  • Target: the target object of the proxy and the object of the enhanced class
  • Weaving: refers to the process of applying enhancements to the target object to create a new proxy object.
  • Proxy: After - classes are enhanced by AOP weaving, a proxy object is generated.
  • Aspect: a combination of pointcuts and notifications (Introductions).

Proxy = target (join point, pointcut) + aspect (notification / enhancement) + weaving

proxy pattern

That is, I generate another proxy class to proxy the saveUser() method of UserController. The code is roughly as follows:

class UserControllerProxy {
    private UserController userController;

    public void saveUser() {
        checkAuth();
        userController.saveUser();
    }
}

In this way, when I actually call saveUser(), I call the saveUser() method of the proxy object to realize authentication.

Agents are divided into static agents and dynamic agents. Static agents, as the name suggests, are agents that you write yourself. Dynamic agents generate a proxy object during runtime.

Open aop annotation

<!--Open the annotation to confirm the scanning range-->
<context:component-scan base-package=""/>
<!--Annotation implementation aop,open aop annotation--><aop:aspect j-autoproxt/>


    

Configuring annotations in slice layers

@Component

@Aspect

Topics: Java JavaEE Spring