springBoot and Micro Services

Posted by JJ2K on Sun, 06 Feb 2022 18:10:15 +0100

Spring

1,Spring

1.1. Introduction:

  • Spring:spring

  • spring concept: making existing technology easier to use is a hodgepot in itself

  • SSH: Struct2+Spring+Hibernat

  • SSM: SpringMVC+Spring+Mybatis

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.13</version>
</dependency>

1.2, Advantages

  • Spring is an open source, free framework (container)
  • Spring is a lightweight, non-invasive framework
  • Control Inversion (IOC), Face Oriented Programming (AOP)
  • Supports transaction processing and framework integration

To summarize, Spring is a lightweight IOC and AOP programming

1.3, Composition

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-w8k6rMeQ-1644161208157) (D:\webgitdocumentassetsimage-2021353985.png)]

1.4, Expansion

It's on Spring's website: modern Java development! To put it plainly, development based on Spring

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-u8fyHoMm-1644161208159) (D:\webgitdocumentassetsimage-202135620031.png)]

  • springboot
    • A fast-developing scaffold
    • Quick development of a single micro-service based on springboot
    • Contract is greater than configuration!
  • springcloud
    • springcloud is based on springboot

Because most companies are using springboot for rapid development, a full mastery of spring and springMVC is required before learning! Connect the role of the next!

Disadvantage: After too long development, it violates the original idea! Configuration is cumbersome, known as "Configure Hell"!

2. Deduction of IOC Theory

1. UserDao interface

2. UserDaoImpl Implementation Class

3. UserService Business Interface

4. UserServiceImpl Business Implementation Class

In our previous business, user needs may affect our original code, we need to modify the original code according to user needs! If you have a very large amount of program code, modifying it once can be costly!

We implemented it using a set interface

// Dynamic implementation of specified injection using set
    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
  • Previously, the program was actively creating objects! Control is in the hands of the programmer!
  • With set injection, the program is no longer active but passive

This idea essentially solves the problem, and our programmers no longer need to manage the creation of objects. The coupling of the system is greatly reduced, so that you can focus more on business implementation! This is the IOC prototype!

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-isyljJK6-1644161208159) (D:\webgitdocumentassetsimage-20223518.png)]

IOC Essence

Inversion of Control is a design idea, DI (Dependent Injection) is a way to implement Ioc, and it is also said that task DI only has IOC. In programs without Ioc, we use object-to-object programming. The dependencies created by objects are completely hard-coded in the program. The creation of objects is controlled by the program itself. After the inversion of control, the creation of objects is transferred to third parties. The so-called inversion of control for personal tasks is: the way to obtain dependent objects is inverted.

When configuring a Bean in xml, the Bean's definition information is separate from the implementation, and the Bean's definition information can be combined with the implementation by annotation. Bean's definition information is directly defined in the implementation class by annotation, thus achieving the goal of zero configuration.

Controlling inversion is a way of producing or acquiring specific objects through descriptions (XML or annotations) and through third parties. Controlling inversion in Spring is the ioc container, which is implemented by Dependency Injection (DI)

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--Use spring To create an object in spring These become Bean
        Hello hello = new Hello();

        id = "Variable Name"
        class  = new Object
        property Equivalent to setting a value to an attribute in an object!
        -->
    <bean id="hello" class="com.kuang.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>
</beans>
		// Get spring Context Object
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // Our objects are managed in spring, we need to use them, just go inside and take them out
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());

[External chain picture transfer failed, source station may have anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img-joDako9p-1644161208160) (D:\webgitdocumentassetsimage-2020144.png)]

Think about it?

  • Who created the Hello object?

    hello objects are created by spring

  • How are the properties of the Hello object set?

    The properties of the hello object are set by the Spring container

This process is called inversion of control:

Control: Who controls the creation of objects. Objects in traditional applications are created under the control of the program itself. With spring, objects are created by spring

Reverse: Instead of creating objects, the program itself becomes a passive recipient

Dependent Injection: Injected using the set method

IOC is a programming idea, from active programming to passive reception

Browse the underlying source code through the new ClassPathXmlApplicationContext

To do different things, you only need to modify it in the xml configuration file, so-called ioc, in one sentence: objects are created, managed, and assembled by spring

3. IOC Create Objects

1. Use parameterless construction to create objects, default!

2. Suppose we want to use a parametric constructor to create a function

1. Subscript Copy

    <!--The first parametric construction-->
    <bean id="user" class="com.kuang.pojo.User" >
        <constructor-arg index="0" value="Ansheng"/>
    </bean>

2. type

 <!--The second is created by type, not recommended!-->
<bean id="user" class="com.kuang.pojo.User">
       <constructor-arg type="java.lang.String" value="Please add"/>
</bean>

3. Parameter Name

<!--Create directly from the parameter name-->
    <bean id="user" class="com.kuang.pojo.User">
        <constructor-arg name="name" value="Ansheng"/>
    </bean>

Summary: By the time the configuration file loads, the objects managed in the container are already initialized!

4. spring Configuration

4.1 Alias

<!--If an alias is added, we can also use it to get this object-->
<alias name="user" alias="user2"/>

4.2 Bean Configuration

<!--
    id: bean The unique identifier of the
    class: bean The fully qualified name of the object; Package Name+type
    name: It's also an alias, and name Multiple aliases can be taken at the same time
    -->
    <bean id="user" class="com.kuang.pojo.User" name="user3,u2">
        <property name="name" value="Ansheng"/>
    </bean>

4.3 import

This import is typically used for team development and can combine multiple profiles into one import

If there are more than one person developing in the project now, these three people copy different class development, different classes need to be registered in different beans, we can use import to make all beans available. XML merge into a total!

<import resource="bean1.xml"/>
<import resource="bean2.xml"/>
<import resource="bean3.xml"/>

5. Dependent Injection

5.1 Constructor Injection

I've said that before

5.2 Set Mode Injection (Key)

  • Dependent Injection: Set Injection
    • Dependency: bean objects are created by containers
    • Injection: Properties in the bean object, injected by the container

[Environment Building]

1.Address.java

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

2.Student.java

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobby;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
}

3.beans.xml

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="student" class="com.kuang.pojo.Student">
        <property name="name" value="Ansheng"/>
    </bean>


</beans>

4. Test Classes

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}

5. Perfect Injection Information

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.kuang.pojo.Address">
        <property name="address" value="Beijing"/>
    </bean>

    <bean id="student" class="com.kuang.pojo.Student">
        <!--1,General Injection-->
        <property name="name" value="Ansheng"/>
        <!--2,bean injection  ref-->
        <property name="address" ref="address"/>
        <!--3,Array Injection-->
        <property name="books">
            <array>
                <value>The Dream of Red Mansion</value>
                <value>Journey to the West</value>
                <value>Water Margin</value>
                <value>Romance of the Three Kingdoms</value>
            </array>
        </property>

        <!--List-->
        <property name="hobby">
            <list>
                <value>Listen to the music</value>
                <value>Knock Code</value>
                <value>Watch movie</value>
            </list>
        </property>

        <!--Map-->
        <property name="card">
            <map>
                <entry key="ID" value="1111111"/>
                <entry key="Bank card" value="11123232"/>
            </map>
        </property>
        
        <!--Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
        
        <!--null-->
        <property name="wife">
            <null/>
        </property>
        
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="School Number">123</prop>
                <prop key="Gender">male</prop>
            </props>
        </property>
    </bean>


</beans>

5.3 Other ways

Microservice Phase

Java: oop

mysql: persistence

html+css+jquery+framework: framework

javaweb: Independently develop MVC three-tier framework

SSM: Framework: Simplify our development process and begin to be more complex to configure

war:tomcat run

spring Resimplification: springBoot-jar

ty name="info">

123
male


```

5.3 Other ways

Microservice Phase

Java: oop

mysql: persistence

html+css+jquery+framework: framework

javaweb: Independently develop MVC three-tier framework

SSM: Framework: Simplify our development process and begin to be more complex to configure

war:tomcat run

spring Resimplification: springBoot-jar

Topics: Java Spring Boot Microservices