This is the 24th day of my participation in Gwen Challenge

Spring is introduced

  1. frame
    1. Test: Spring provides testing capabilities
    2. Core Container: indicates the Core Container. The most basic conditions for Spring startup.
      • Beans: Spring is responsible for creating class objects and managing them
      • The Core: the Core classes
      • Context: Context parameter. Get external resources or manage annotations
      • SpEl: expression.jar
    3. AOP: Implementing AOP functionality requires dependencies
    4. Aspects: Packages that facet AOP dependencies
    5. Data Access/Integration: Spring encapsulates the Data Access layer
      • JDBC: Spring’s code for JDBC encapsulation
      • ORM: Code that encapsulates the persistence layer framework. For example, Hibernate
      • Transactions: corresponds to Spring-tx.jar, used for declarative transactions.
    6. WEB: Required when Spring is required to complete Web-related functions
      • For example, the Spring-Web package is required to load the Spring configuration file by Tomcat
  2. Several core Spring features
    1. IoC/DI Inversion of control/dependency injection
    2. AOP is faceted oriented programming
    3. Declarative transaction
  3. Environment set up
    1. Spring configuration files are based on schema
    2. Every time you use one of Spring’s features, you put the corresponding XSD file address into the configuration file
    Search for it in official documents1.Gets bean-related XMLNS2.Get AOP-related XMLNS: AOP3. Other analogiesCopy the code
    1. The Spring container ApplicationContext, ApplicationContext. XML configuration information stored in AppliationContext container finally

Ii. IOC(Inversion of Control)

  1. Inversion of control
    • Control: Controls the objects of the class
    • Inversion: Objects are handed over to Spring
  2. role
    1. What IoC does was previously handed over to Spring by the programmer via the new instantiation object thing
    2. Decoupling.
      • Programmers do not need to manage objects. Decouple object management from the programmer.
  3. Why not just new objects instead of Spring injection?
    • Spring implements object pooling. Some objects are not destroyed after they are created and used. They are put into an object pool (some kind of collection) for later use. Save time and CPU.
  4. Create objects in the Spring configuration file and get objects in the test class
    1. create
      • Id: The bean’s identity
      • Class: Which class the bean belongs to
    
            
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/sc
    hema/beans
    http://www.springframework.org/schema/beans/spring-be
    ans.xsd">
    <! -- id specifies the object id of the class to be created -->
    <bean id="peo" class="com.bjsxt.pojo.People"/>
    </beans>
    Copy the code
    1. Get a bean object in the test class
    GetBean (" <bean> tag ID value ", return value type); Without the second argument, the default is Object ApplicationContext AC =new
    ClassPathXmlApplicationContext("applicationContext.xml");
    People people = ac.getBean("peo",People.class);
    System.out.println(people)
    Copy the code
    1. Get all the objects in the Spring container
    String[] names = ac.getBeanDefinitionNames();
    for (String string : names) {
    System.out.println(string);
    Copy the code

Spring creates objects in three ways(back top)

Create by constructor

  1. The default constructor is to create an object with no arguments
    • That is, creating a bean for a class uses its parameterless method
  2. Use the parameterized constructor of the class
    1. Class to provide the parameterized constructor
    2. Do the configuration in the bean of the configuration file
      • If the condition is set to match more than one constructor, the last constructor in the class is used
      • Use a labelconstructor-arg, contains three attributes
        • Index: indicates the index of the parameter, starting from 0
        • Name: parameter name
        • Type: type (distinguish keywords from encapsulation classes int and Integer)
    <bean id="peo" class="com.bjsxt.pojo.People">
    <! -- ref references another bean value base datatype, String, etc -->
    <constructor-arg index="0" name="id" type="int"
    value="123"></constructor-arg>
    <constructor-arg index="1" name="name"
    type="java.lang.String" value="Zhang"></constructor-arg>
    </bean>
    Copy the code

Example Factory pattern

  1. First you need an instance factory class
public class PeopleFactory {
    public People newInstance(a){
    return new People(1."Test");}
    }
Copy the code
  1. Set in the configuration file
    • (1) Create the bean of the factory class
    • (2) Create objects
      • Id Sets the identity of the object to be created
      • Sets the factory class to use
      • Sets which method of the factory class is used to create the object
<bean id="factory"
class="com.bjsxt.pojo.PeopleFactory"></bean>

<bean id="peo1" factory-bean="factory"
factory-method="newInstance"></bean>
Copy the code

Static factory

  1. And the factory model
    • Because it is static, you can call the method directly without creating a Factory object
    • You don’t have to create a factory bean
  2. Create a static factory
public class PeopleFactory {
    public static People newInstance(a){
    return new People(1."Test"); }}Copy the code

3 Configure -id in the configuration file. -class: indicates the full path of the factory. -factory-method: indicates the method of the factory

<bean id="peo2" class="com.bjsxt.pojo.PeopleFactory"
factory-method="newInstance"></bean>
Copy the code

Assign a value to the bean’s properties (simple data types)(back top)

  1. The object is created using the parameterized constructor above and then assigned
  2. Set the injection
    • Getter /setter methods must be set for simple types in a class
    1. If the attribute is a primitive data type or String
    # 1: form a<bean id="peo" class="com.bjsxt.pojo.People">
        <property name="id" value="222"></property>
        <property name="name" value="Zhang"></property>
    </bean># 2: two forms<bean id="peo" class="com.bjsxt.pojo.People">
        <property name="id">
            <value>456</value>
        </property>
        <property name="name">
            <value>zhangsan</value>
        </property>
    </bean>
    Copy the code
    1. If the property is set
    <property name="sets">
        <set>
            <value>1</value><value>2</value>
            <value>3</value>
            <value>4</value>
        </set>
    </property>
    Copy the code
    1. If the attribute is List
    #1: List has multiple values<property name="list">
        <list>
            <value>1</value>
            <value>2</value>
            <value>3</value>
        </list>
    </property>#1: List has only one value<property name="list" value="1">
    </property>
    Copy the code
    1. If the property is an array
    <property name="strs" >
        <array>
            <value>1</value>
            <value>2</value><value>3</value>
        </array>
    </property>
    Copy the code
    1. If the attribute is map
    <property name="map">
        <map>
            <entry key="a" value="b" ></entry>
            <entry key="c" value="d" ></entry>
        </map>
    </property>
    Copy the code
    1. If the property is of type Properties
    <property name="demo">
        <props>
            <prop key="key">value</prop>
            <prop key="key1">value1</prop>
        </props>
    </property>
    Copy the code

Assign a value to the bean’s properties (object type)

  1. Simply put, this is used when the attribute is of a simple typevalueThe assignment
  2. Property is an object typerefThe assignment
// Create an object of the Desk class with the id and price attributes<bean id="desk" class="com.bjsxt.pojo.Desk">
<property name="id" value="1"></property>
<property name="price" value="12"></property>
</bean>// Create an object of class People with an object of type Desk<bean id="peo" class="com.bjsxt.pojo.People">
<property name="desk" ref="desk"></property>
</bean>
Copy the code