Spring framework learning path, the full version is continuously updated, with code warehouse comparison

Posted by squariegoes on Tue, 28 Dec 2021 16:18:04 +0100

✅ Spring framework learning

   this project mainly summarizes my learning process of Spring framework, which will include some experience, source code practice, and my understanding of some knowledge points, so as to facilitate continuous learning, timely and efficient review and review on the way of continuous learning, and summarize the understanding of initial contact learning and subsequent review, It can better sort out the knowledge framework system.

❤️ Simple understanding of Spring

  • Spring is an open source lightweight framework
  • It mainly solves the problem of enterprise development
  • Spring can be regarded as a container and is non intrusive
  • Face section
  • IOC(Inversion of Control) and AOP(Aspect Oriented Programming)

♥️ Summary of knowledge points

first ️⃣ day

  • Spring framework construction
  • Basic Dao layer (data access layer), Service layer (business layer logic) and user layer architecture
  • Reduce the coupling of the program
  • Using the Set() method for injection, the program gives the user the power to create objects according to the business, so that the user can create the desired objects according to the needs. The program only passively accepts the objects and gives the power to the user.

🎈 Personal understanding
IOC: control inversion is a design idea, that is, the source of obtaining dependent objects has changed. In general, the program actively creates the required business objects. After the control reversal, the creation power of dependent objects is handed over to the user, so that the user can create different objects according to their own needs without affecting the architecture of the program system itself and reducing the coupling degree. In the first day of study, we used di (dependency injection) to implement it. We called the object actively built by the user into the program through the middle Set method to realize the inversion of control.

The function of IOC is to reduce the coupling of complex and highly coupled objects through the IOC container, so that all parts of the can operate independently and try not to interfere with each other, that is, the abnormality of one party will not affect other aspects, reduce the coupling, and improve the convenience of maintaining the project and expanding the existing functions of the project.

second ️⃣ day

  • IOC is the core content of Spring framework
  • IOC can also be implemented by configuring beans through XML. The definition information and implementation of beans are separated
  • When initializing, the Spring container reads the configuration file first and stores all the organization objects in the configuration file into the container.

    Note: in Spring, as long as the configuration file is loaded, no matter whether the class gets or calls the object in the container, the object will be defined in advance. It is only used when it is used, not when it is used.

  • Objects are created, managed and assembled by Spring.

💥 xml and bean syntax

  1. Declare XML configuration, declare bean

XML declaration: the format officially given by 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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="..." class="...">
     <!-- collaborators and configuration for this bean go here -->
  </bean>
  
  <bean id="..." class="...">
     <!-- collaborators and configuration for this bean go here -->
  </bean>
  <!-- more bean definitions go here -->
</beans>
  1. Load the configured XML file into the actually used class, and the context instance object contains all the bean s declared in the XML file.
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
  1. Syntax definition of the actual bean declaration

The main methods of creating objects will be recorded tomorrow

<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
</bean>

Notes:

id:ID is the name of the object contained in the context object that instantiates the type declared in the current Bean. You can use context GetBean ("fill in the declared ID value here to get the object");

Class: it is the class managed by the current bean tag. The absolute position needs to be filled in, that is, the type of the constructed object is the type of the current declaration

Property: initialization of class object properties, where name is the property name in the class. If the property value is the basic data type, value is used for assignment. If it is other data types, ref is used for assignment.

Object construction in bean: when there is no call, the nonparametric constructor has been called, that is, the object has been called in the initialization loading phase

Load additional XML in XML file

    in the XML file, you can import other XML configuration files through the import tag. The syntax is as follows.

In a project, it may be divided into multiple modules, and multiple modules will create different beans XML file is inconvenient to import, so it can be imported for our convenience.

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

   in the contract, users can define multiple XML files, but it is cumbersome to import all of them into class files in the process of use. Therefore, we generally import all XML files into ApplactionXml. When using, we only need to import this one.

third ️⃣ day

  • The Spring container can be regarded as an object pool. All the defined objects are stored and then called through the Spring container.
  • Several ways IOC creates objects
  • Alias bean objects

IOC creates objects in several ways:

  1. Call parameterless constructor

Assign values to attributes directly

<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
        <!-- additional collaborators and configuration for this bean go here -->
</bean>
  1. Call parameterized constructor
    Using the constructor Arg tag, you can transfer parameters.
  • Pass the reference to the constructor and pass the parameters in the order of labels (not recommended)
 <beans>
    <bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg ref="beanTwo"/>
        <constructor-arg ref="beanThree"/>
    </bean>
    
    <!--  The reference object passed into the constructor above needs to be used separately bean Label to declare  -->
    <bean id="beanTwo" class="x.y.ThingTwo"/>

    <bean id="beanThree" class="x.y.ThingThree"/>
</beans>
  • Pass the given data type, and then pass in the value value directly
<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg type="int" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/>
</bean>
  • Pass the subscript of the given parameter list, and then pass in the value value

The subscript index is consistent with the rules in Java, starting from 0

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
</bean>
  • Pass parameters by constructor parameter names to avoid ambiguity (recommended)

Name is the constructor parameter name, and value is the specific value passed in

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg name="years" value="7500000"/>
    <constructor-arg name="ultimateAnswer" value="42"/>
</bean> 

Alias bean objects

In actual use, after creating a large number of objects, the names of some objects may be repeated or very complex, which is inconvenient for us to call. You can use aliases.

  1. Alias with alias tag

name is the id value of the specified bean object, and alias is the value of the alias to be set

<alias name="fromName" alias="toName"/>
  1. Alias directly in the bean tag
<bean id="bean1" class="..." name="beiming"/>

fourth ️⃣ day

  • Initialization of common data structures (Connection, Map, Set, ` `)

Topics: Java Spring Programmer