[back end integration] new program Day3

Keywords: Front-end html css

1. Explanation of spring framework

1.1 single case and multiple cases

1.1.1 description of single case and multiple cases

  1. Singleton: there is only one singleton in memory (even if it is not created, the system defaults to singleton)
  2. Multiple examples: there may be multiple copies in memory

1.1.2 create project springdemo5_base  

Refer to spring demo 3-anno to create

  1.1.3 edit User class

package com.jt.demo;

import org.springframework.stereotype.Component;

public class User {
    public User(){
        System.out.println("I am a nonparametric structure,create object");
    }

    public void say(){
        System.out.println("Is the test object a single case or multiple cases");
    }
}

1.1.4 test on single case and multiple cases

Rule 1: all objects managed by Spring by default are singletons.
Rule 2: control single instance / multiple instances of objects through @ Scope annotation

package com.jt.config;

import com.jt.demo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration  //Identifies that this is a configuration class
@ComponentScan("com.jt")
public class SpringConfig {

    @Bean
    @Scope("singleton")     //Default singleton mode
    //@Scope("prototype") / / multi instance mode
    public User user(){
        return new User();
    }
}

  1.1.5 edit test class

package com.jt;

import com.jt.config.SpringConfig;
import com.jt.demo.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {


    @Test
    public void testDemo1(){
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        User user1 = context.getBean(User.class);
        User user2 = context.getBean(User.class);
        user1.say();
        System.out.println(user1 == user2);//true
    }
}

1.2 lazy loading mechanism  

1.2.1 lazy loading instructions

Note: if the Spring container is created and the object is created immediately, the loading method is "load now", and the container starts creation
If the Spring container is created and the object is created only when it is used, it is called "lazy loading" and is created only when it is used

Note: @ Lazy added indicates Lazy loading
Test Description: when will the nonparametric structure in the main test object be executed!!!

  1.2.2 lazy loading usage

package com.jt.config;

import com.jt.demo.User;
import org.springframework.context.annotation.*;

@Configuration  //Identifies that this is a configuration class
@ComponentScan("com.jt")
public class SpringConfig {

    @Bean
    //@Scope("singleton") / / default value: Singleton mode
    //@Scope("prototype") / / multi instance mode
    @Lazy                    //Lazy loading
    public User user(){
        return new User();
    }
}

1.2.3 relationship between multiple cases and lazy loading

Note: as long as the object is in multi instance mode, it is lazy loading!, Controlling lazy loading is only effective in singleton mode
Rule Description:
lazy true lazy false
Singleton mode: valid lazy loading valid immediate loading
Multi instance mode: invalid lazy load invalid lazy load

  1.2.4 description of lazy usage scenario

Scenario 1: when the server is started, if too many resources are loaded, the server will start slowly. Set the unimportant resources as lazy loading appropriately
Scenario 2: sometimes users need some special "links", and the creation of these links takes a long time. Lazy loading can be used

1.3 Spring object lifecycle management  

1.3.1 description of object life cycle

Note: an object can be divided into four stages from creation to extinction. If you need to intervene in the program, you can intervene through the periodic method. (callback function / hook function / interface callback)
Illustrate the function of life cycle function: the main function can intervene the object in each period

  1.3.2 use of life cycle letter method

package com.jt.demo;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component  //Give the object to the Spring container management key=person value: reflection object
public class Person {

    public Person(){
        System.out.println("Zhang San was born,Qualification pull full");
    }

    @PostConstruct  //Called immediately after the object is created
    public void init(){
        System.out.println("Zhang San is called a young wizard");
    }

    //Business method
    public void doWork(){
        System.out.println("Marry a mermaid!!!");
    }

    @PreDestroy //Called when the object dies
    public void destory(){
        System.out.println("Destroy:World mourning");
    }
}

1.3.3 test cases  

@Test
    public void testDemo3Init(){
        //Container start object creation
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        Person person = context.getBean(Person.class);
        person.doWork();
        //Close the container
        context.close();
    }

1.3.4 use notes  

  1. @PostConstruct / / called immediately after the object is created

  2. @PreDestroy / / called when the object dies

1.2 Dependency Injection (DI)  

1.2.1 create structure  

  1. Prepare User class
  2. Prepare Dog class
  3. Prepare Cat class
    Description: Dog/Cat performs object injection to User class

 

  1.2.2 @Autowired annotation

Note: if attribute injection is required in an object, the @ Autowired annotation is generally used
Function: you can automatically inject objects in Spring container into properties
Injection method:
1. Inject by type by default. If the injected attribute is an interface, the implementation class will be injected automatically
2. Inject (key) by name. Generally not used

Important premise:   If dependency injection is required, the object must be managed by the Spring container

  1.2.3 edit Pet interface

public interface Pet {
    void hello();
}

1.2.4 editing Cat classes  

package com.jt.demo;

import org.springframework.stereotype.Component;

@Component //Give the object to spring container management key:cat value: reflect Cat object
public class Cat implements Pet{

    @Override
    public void hello() {
        System.out.println("I'm meow meow");
    }
}

1.2.5 edit User class

package com.jt.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component  //Leave user to Spring container management
public class User {

    //Effect: automatically inject the implementation class of the current interface
    @Autowired
    private Pet pet;

    public void say(){
        //Call Pet's method
        pet.hello();
    }
}

1.2.6 edit configuration class  

package com.jt.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration            //Identify me as a configuration class
@ComponentScan("com.jt")  //Package path must be added
public class SpringConfig {

}

1.2.7 edit test class

package com.jt;

import com.jt.config.SpringConfig;
import com.jt.demo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

class TestSpring {

    @Test
    public void testDemo1(){
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        User user = context.getBean(User.class);
        user.say();
    }

}

1.3 description of multi interface implementation  

1.3.1 code description

1. Cats  

  2. Dogs  

1.3.2 description of error reporting

explain:   An interface should have only one implementation class, otherwise the spring program cannot choose  

  1.3.3 solution

package com.jt.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component  //Leave user to Spring container management
public class User {

    //Effect: automatically inject the implementation class of the current interface
    @Autowired
    @Qualifier("cat") //This annotation cannot be used alone. It must be used with Autowired and injected according to the key
    private Pet pet;  //1 out of 2

    public void say(){
        //Call Pet's method
        pet.hello();
    }
}

  1.3.4 @Resource notes

1.4 MVC design idea  

1.4.1 MVC idea description

In the classic MVC mode, m refers to the business model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation codes of M and V, so that the same program can use different forms of expression. Among them, the definition of View is relatively clear, that is, the user interface.

M: model business model
5: View user interface
C: control layer

Historical Description:
JSP dynamic page html code + java code written together xxx.jsp
It is inconvenient for later maintenance. Pages and business execution are tightly bound together, with high coupling

  Summary:
1. MVC is a design idea, which reduces the coupling of code in coding
2. The front end focuses on developing page view s
3. The backend focuses on developing the backend model
4. Both parties control through control

1.4.2 hierarchical code structure

Note: MVC design idea realizes the loose coupling between the front end and the back end. However, according to the actual development situation, many business logic are more complex. If the back end writes all the code to the same java class, the code structure is very bloated   In order to realize the MVC design idea well, the back-end code should also be layered

  Layered Description:
1. The control layer Controller interacts with the front-end page
2. Edit business logic at the business layer. @ Service
3. Persistent layer Mapper implements database related operations temporarily: @ Repository
MVC > three-tier code structure!!!

1.5 three tier code structure  

1.5.1 edit Mapper layer

Posted by xcali on Tue, 30 Nov 2021 21:46:55 -0800