Spring boot framework final exam outline explanation

Keywords: Mybatis Spring Spring Boot Spring MVC

Question type

  • choice question
  • Program reading questions
  • Program error correction
  • Procedural blank filling
  • Programming problem

Master the meaning of common coordinate dependency, such as web, hot deployment, etc

<dependencies>
	<dependency>
        <groupId>Project group ID</groupId>
        <artifactId>Project identification</artifactId>
        <version>Project version number</version>
        <scope>Scope</scope>
    </dependency>
</dependencies>

spring boot

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M3</version>
</parent>

spring-boot-starter-web

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Hot deployment

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>2.3.12.RELEASE</version>
    <optional>true</optional>
</dependency>

All taught notes and their meanings and usage;

Launcher annotation

@SpringBootApplication

This annotation is Spring Boot The core annotation is used in Spring Boot On the main class of, identify that this is a Spring Boot Application to open Spring Boot Various abilities. In fact, this annotation is@Configuration,@EnableAutoConfiguration,@ComponentScan A combination of three annotations. Because these annotations are generally used together, so Spring Boot Provides a unified annotation@SpringBootApplication. 
    
@ComponentScan

Component scan. Give Way spring Boot Scan to Configuration Class and add it to the program context.
@ComponentScan The annotation will be marked by default@Controller,@Service,@Repository,@Component Annotated class to spring In the container.

Component registration annotation

@Component : The standard is an ordinary one spring Bean Class. 
@Repository: Label a DAO Component class. 
@Service: Annotate a business logic component class. 
@Controller: Label a controller component class. 

java explicit assembly annotation

@Configuration

Used to define the configuration class, indicating that the class is Bean The configured information source is equivalent to the traditional information source xml Configuration files are usually added to the main class. If some third-party libraries need to be used xml Document, recommendation still adopted@Configuration Class as the main configuration class of the project - you can use@ImportResource Annotation loading xml Configuration file.

@Bean

amount to XML Medium,Put it above the method, not the class, which means to generate a bean,And give it to spring Administration.

example

@Configuration  //Represents that this is a configuration class
@Import(MyConfig2.class)  //Import and merge other configuration classes, similar to the inculde label in the configuration file
public class MyConfig {
	/**
     *    Register a bean through the method, method name = bean.id; Return value = bean.class
     */
   @Bean //Register a bean through method
   public Dog dog(){
       return new Dog();
  }
}

mvc annotation

@RestController

Used to label control layer components(as struts Medium action),Indicates that this is a controller bean,And the return value of the function is directly filled in HTTP In response body,yes REST Style controller; It is@Controller and@ResponseBody A collection of.
    
@RequestMapping

RequestMapping Is an annotation used to handle request address mapping; Provide routing information and be responsible for URL reach Controller The mapping of specific functions in can be used on classes or methods. vaule=""Equivalent to path="",Can be omitted.
    
@ResponseBody

Indicates that the return result of the method is written directly HTTP response body in

@RequestBody

Means that the method parameter is bound to HTTP request Body On, the front-end can't submit in the form. You need to use json Submitted by.
GET The method has no request body, so it is used@RequestBody When receiving data, the front end cannot be used GET Instead of submitting data in POST Submit by.
In the same receiving method at the back end,@RequestBody And@RequestParam()It can be used at the same time,@RequestBody There can only be one at most, and@RequestParam()There can be more than one.

@RequestParam

Used in front of method parameters
value = "" Front end parameter name
required = false Is it necessary
defaultValue = "" Default value
    
@PathVariable

adopt @PathVariable Can URL Placeholder parameters in{xxx}Bound to a method parameter of a processor class@PathVariable("xxx") 

Auto inject annotation

@AutoWired

byType Way. Configure the Bean It can label class member variables, methods and constructors to complete the work of automatic assembly.

When you add( required=false)Even if you can't find it bean No error.

@Qualifier

When there are multiple of the same type Bean You can use@Qualifier("name")To specify@Autowired Use together

@Resource(name="name",type="type")

If there is no content in parentheses, the default is byName. And@Autowired Do something similar.

Notes for understanding only

@ConfigurationProperties

Spring Boot You can annotate the user-defined properties Mapping files to entities bean In, for example config.properties File.

@Value("${app.name}")
Injection simple value
 
@Import

The instance is added by importing springIOC In container

@EnableAutoConfiguration

Give Way Spring Boot Based on the dependencies declared by the application Spring The framework is automatically configured and generally added to the main class.

@Profiles
    
Spring Profiles Provides a way to isolate application configurations and make them effective only in a specific environment.
whatever@Component or@Configuration Can be@Profile Tag, which limits the timing of loading it.

Understand and apply the principle and injection mode of IOC, and the principle and application of AOP;

IoC (reverse control):

​ One design idea is to hand over the control of manually creating objects in the program to the Spring framework

DI (dependency injection):

​ Dependency injection: when the Spring framework is responsible for creating Bean objects, it dynamically injects dependent objects into Bean components.

Three methods of attribute injection

Constructor Injection

Nonparametric structure
 <bean id="user" class="com.kuang.pojo.User"/>
Parametric structure
<!-- First basis index Parameter subscript setting -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <!-- index Refers to the construction method , Subscript starts at 0 -->
    <constructor-arg index="0" value="kuangshen2"/>
</bean>
<!-- The second is set according to the parameter name -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <!-- name Refers to the parameter name -->
    <constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- The third is set according to the parameter type -->
<bean id="userT" class="com.kuang.pojo.UserT">
    <constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>

​ value: Specifies the basic data type or String data type

​ ref: specifies data of other bean types

Setter method injection

to configure

  • pojo
 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         this.address = address;
    }
 }
public class Student {
 
     private String name;
     private Address address;
     private String[] books;
     private List<String> hobbys;
     private Map<String,String> card;
     private Set<String> games;
     private String wife;
     private Properties info;
 
     public void setName(String name) {
         this.name = name;
    }
 
     public void setAddress(Address address) {
         this.address = address;
    }
 
     public void setBooks(String[] books) {
         this.books = books;
    }
 
     public void setHobbys(List<String> hobbys) {
         this.hobbys = hobbys;
    }
 
     public void setCard(Map<String, String> card) {
         this.card = card;
    }
 
     public void setGames(Set<String> games) {
         this.games = games;
    }
 
     public void setWife(String wife) {
         this.wife = wife;
    }
 
     public void setInfo(Properties info) {
         this.info = info;
    }
 
     public void show(){
         System.out.println("name="+ name
                 + ",address="+ address.getAddress()
                 + ",books="
        );
         for (String book:books){
             System.out.print("<<"+book+">>\t");
        }
         System.out.println("\n hobby:"+hobbys);
 
         System.out.println("card:"+card);
 
         System.out.println("games:"+games);
 
         System.out.println("wife:"+wife);
 
         System.out.println("info:"+info);
 
    }
 }
Injection constant
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="Xiao Ming"/>
 </bean>
Inject bean (base)

​ The value here is a reference, ref  

 <bean id="addr" class="com.kuang.pojo.Address">
     <property name="address" value="Chongqing"/>
 </bean>
 
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="Xiao Ming"/>
     <property name="address" ref="addr"/>
 </bean>
Injection array
 <bean id="student" class="com.kuang.pojo.Student">
     <property name="name" value="Xiao Ming"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>Journey to the West</value>
             <value>The Dream of Red Mansion</value>
             <value>Water Margin</value>
         </array>
     </property>
 </bean>
Inject List
<property name="hobbys">
     <list>
         <value>listen to the music</value>
         <value>watch movie</value>
         <value>Mountain climbing</value>
     </list>
 </property>
Inject Map
<property name="card">
     <map>
         <entry key="China Post" value="456456456465456"/>
         <entry key="build" value="1456682255511"/>
     </map>
 </property>
Injection Set
 <property name="games">
     <set>
         <value>LOL</value>
         <value>BOB</value>
         <value>COC</value>
     </set>
 </property>
Inject Null
<property name="wife"><null/></property>
Inject Properties
 <property name="info">
     <props>
         <prop key="Student number">20190604</prop>
         <prop key="Gender">male</prop>
         <prop key="full name">Xiao Ming</prop>
     </props>
 </property>

P named and C named injection

P namespace (class Set injection, must have Set method)

<bean id="john-modern" class="com.example.Person" p:name="John Doe" p:spouse-ref="jane"/>
<!--Equivalent to-->
<bean id="john-classic" class="com.example.Person">
    <property name="name" value="John Doe"/>
    <property name="spouse" ref="jane"/>
</bean>

C namespace (class constructor injection, must have parameter construction)

<bean id="beanOne" class="x.y.ThingOne" 
        c:thingTwo-ref="beanTwo"  
        c:thingThree-ref="beanThree" 
        c:email="something@somewhere.com"/>
<!--recommend-->
<!--Equivalent to-->
<bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg name="thingTwo" ref="beanTwo"/>
        <constructor-arg name="thingThree" ref="beanThree"/>
        <constructor-arg name="email" value="something@somewhere.com"/>
</bean>
<bean id="beanOne" class="x.y.ThingOne" 
    c:_0-ref="beanTwo" 
    c:_1-ref="beanThree"
    c:_2="something@somewhere.com"/>
    <!--Equivalent to-->
<bean id="beanOne" class="x.y.TingOne">
    <constructor-arg index="0" ref="beanTwo"/>
    <constructor-arg index="1" ref="beanThree"/>
    <constructor-arg index="2" value="something@somewhere.com"/>
</bean>

Annotation injection

@Component("user")
// Equivalent to < bean id = "user" class = "currently annotated class" / > in the configuration file
public class User {
    @Value("Qin Jiang")
    // Equivalent to < property name = "name" value = "Qin Jiang" / > in the configuration file
    public String name;
}
@Component("user")
public class User {
 
    public String name;
 
    @Value("Qin Jiang")
    public void setName(String name) {
        this.name = name;
    }
}

AOP

Aspect oriented programming: extract the repeated code of the program, and when it needs to be executed, use the dynamic agent technology to enhance the existing methods without modifying the source code.

​ AOP function: during the running of the program, the existing methods are enhanced without modifying the source code.

​ AOP advantages: reduce duplicate code, improve development efficiency and facilitate maintenance.

​ Notification: security, transaction, log, etc. it is divided into five types: Before, After returning, After throwing, After and Around.

Join points are places where spring allows you to use notifications. Basically, the front and back of each method (both are OK), or when throwing exceptions, can be join points. Spring only supports method join points

Pointcut defines the pointcut based on the connection point mentioned above

Aspect aspect is a combination of notification and pointcuts.

Target represents the target object (proxied object). An object notified by one or more facets.

Proxy refers to the proxy object. It is an object created dynamically after the notification is applied to the target object. It can be simply understood that the proxy object is an object formed by the business logic function of the target object and the cut in section.

Weaving represents a cut in, also known as weaving. The process of applying a cut to the target object to create a new proxy object

Configuring aop with spring.xml

		<!-- to configure service -->
    <bean id="customerService" class="nuc.edu.cn.service.impl.CustomerServiceImpl"></bean>
         <!-- be based on xml of aop The configuration step must be imported aop of jar package -->

         <!-- Step 1: give the notification class to spring To manage -->
    <bean id="customerAspect" class="nuc.edu.cn.advice.CustomerAspect"></bean>
 
         <!-- Step 2: Import aop Namespace, and use aop: config start aop to configure -->
    <aop:config>
         <!-- Step 3: use aop: aspect Configure section ,id Property is used to provide a unique identification for the cut, ref For reference notifications bean of id -->
    	<aop:aspect id="customerAdvice" ref="customerAspect">
         <!-- Step 4: configure the notification type and specify when the enhanced method is executed. method: Specifies the enhanced method name -->
		<!-- Pointcut expression: keyword: execution(expression). Expression writing: access modifier return value package name.Package name...Class name.Method name (parameter list) pointcut="execution(* *..*.*(..))" -->
    		<aop:before method="before" pointcut="execution(public void nuc.edu.cn.service.impl.CustomerServiceImpl.saveCustomer())" />
         </aop:aspect>
	</aop:config>

Master MVC application methods, and be able to use automatic injection to realize class hosting;

xml Automatic Injection

You can add the attribute autowire="byName" or autowire="byType" to the bean node, which will automatically find the corresponding bean injection in the spring container, eliminating the trouble of manual injection.

Automatic annotation injection

Using annotations

@Autowired

  • The default is automatic assembly by type. Multiple matching, assembly by byName.
  • @Qualifier can modify the value of byName

@Resource

  • If both name and type are specified, the only matching bean will be found in the Spring context for assembly. If not, an exception will be thrown
  • If name is specified, the bean with matching name (id) will be found in the Spring context for assembly. If it is not found, an exception will be thrown
  • If type is specified, the only bean with matching type will be found in the Spring context for assembly. If it cannot be found or multiple beans are found, an exception will be thrown
  • If neither name nor type is specified, the assembly will be performed automatically by byName. If there is no match, it will fall back to an original type for matching. If it matches, it will be assembled automatically.

Master the common configuration items of mybatis, and be able to use global and mapping file methods, as well as annotation methods to use mybatis;

Master the use methods of front-end basic controls and forms, and be able to interact with front and back ends;

Posted by Drumminxx on Wed, 01 Dec 2021 14:57:18 -0800