Three frameworks of SSM

Keywords: Spring Spring Boot

1, Basic structure of three frames

1. Why is a framework needed

Note: if the projects in the production environment are developed from scratch (from the bottom), it is too difficult and the development efficiency is extremely low. Therefore, in order to quickly deploy the project online, some specific functions are encapsulated in advanced. If we need to use the encapsulated API, we must code according to other people's requirements

2. Classification of framework:

1.Spring framework: responsible for "macro-control" (leading) in the whole framework and integrating other third-party frameworks

2. Spring MVC framework: it is mainly responsible for the interaction of front and rear data

3.Mybatis framework / MybatisPlus framework: persistence layer framework, which simplifies the way JDBC operates the database and improves efficiency

4.SpringBoot framework / tools: springboot encapsulates the previous framework in a more simplified way, making the program easier

3. Framework call flow chart

  2, Explanation of Spring framework

1. Introduction to spring

Spring framework is an open source J2EE application framework initiated by Rod Johnson. It is a lightweight container for managing the life cycle of bean s.
Spring solves many common problems encountered by developers in J2EE development, and provides powerful functions such as IOC, AOP and Web MVC. Spring can be used to build applications alone, combined with many Web frameworks such as Struts, Webwork and Tapestry, and combined with desktop applications such as Swing. Therefore, spring can be applied not only to JEE applications, but also to desktop applications and applets. The spring framework is mainly composed of seven parts: Spring Core, Spring AOP, Spring ORM, Spring DAO, Spring Context, Spring Web and Spring Web MVC.
Summary: the Spring framework is a lightweight container for managing the life cycle of bean s. The core technologies are IOC and AOP

2.Spring-IOC

1.IOC introduction

The full name of Ioc is Inversion of Control, that is, "control reversal", which is a design idea. Object creation is done by the Spring framework. The lifecycle of objects is managed by the container.

  Summary:

① The original objects are created manually by the user. This method has high coupling. If the class changes, the code will change.

② Now all objects are managed by the spring container. Users do not need to care about how the object is instantiated. The container is responsible for object injection,   There is little need to modify any code in the future,   Reduces code coupling.

2. Create User class

package com.jt;

public class User {
    
    public void say(){
        System.out.println("I am User Object, by Spring Container management");
    }
}

3. Edit the spring.xml configuration file

Note: since you need to use the spring framework, you need to prepare the spring configuration file

Create the spring.xml file in the resources folder

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
        Explanation of knowledge points:This profile is used to manage objects
            Terminology: bean  cover spring Container managed objects are called bean
            Attribute description:
                  id: yes spring Unique identifier of the object in the container. It cannot be repeated
                  class: Full path of object
    -->
    <bean id="user" class="com.jt.demo.User"></bean>
</beans>

4. Edit test class

package com.jt;


import com.jt.demo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

    @Test
    public void TestDemo1(){
        String resource = "spring.xml";
        //Create a spring container and load the specified configuration file. The object has been handed over to the container management
        ApplicationContext context = new ClassPathXmlApplicationContext(resource);
        //Get object from container method 1 get object according to ID
        User user1 = (User) context.getBean("user");
        //Get data by type
        User user2 = context.getBean(User.class);
        user1.say();
    }
}

  Test results:

3. Description of spring container

Explanation: the data structure of the spring container is a Map set, Map < key, value >,

key = "value of id in bean", value = "object instantiated through reflection mechanism"

  4. Understand the reflection source code

Note: the reflection mechanism is widely used in the framework. The object can be obtained by a given type of path, but it is required to have no parameter structure, otherwise the program will report an error.

When the reflection method creates an object, it must call the nonparametric construction of the object!!!
    @Test
    public void TestDemo2() throws Exception{
        User user =(User) Class.forName("com.jt.demo.User").newInstance();
        user.say();
    }

3, Spring annotation development

1. Edit User class

package com.jt.demo;

public class User {
    public void say(){
        System.out.println("Use full annotation");
    }
}

2. Edit configuration class

package com.jt.config;

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

@Configuration //Identifies the current class as a configuration class
public class SpringConfig {//xml
    /**1.xml form
     *      <bean id="user" class="com.jt.demo.User"></bean>
     * 2.Annotation form
     *      Map Organization map of collection < method name, return value of method >
     */
    @Bean
    public User user(){
        return new User();//Reflection mechanism
    }


}

3. 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.annotation.Configuration;

@Configuration
public class TestSpring {
    //Manage objects with annotations
    @Test
    public void testDemo1(){
        //1. Start the spring container with annotations
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        //2. Get object from container
        User user = context.getBean(User.class);
        //3. Object calling method
        user.say();

    }
}

4, Factory mode

2. Create objects using factory mode

1. Business description

2. Create workshop mode

Posted by danlindley on Thu, 25 Nov 2021 13:07:30 -0800