1.IOC

  1. What is the IOC

    It’s not an Ioc — Inversion of Control. It’s a design idea. In Java development, Ioc means handing your designed objects over to the container for control, rather than the traditional direct control within your objects.

  2. The IOC principle

    XML parsing factory schema reflection

    The original way

The factory pattern

Create objects with factory mode: Use the static keyword to create the static method getDao, which can be called directly from the class name, and the UserService obtains the UserDao object from the factory. This pattern reduces coupling between the Service and Dao.

2. IOC (interface)

1. The idea of IOC is based on IOC container. The bottom layer of IOC container is object factory

2.Spring provides two interfaces implemented by the IOC container:

  • BeanFactory: Interface used internally by Spring. Objects are not created when loading configuration files, but are used when fetching objects

  • ApplicationConetext: a subinterface to the BeanFactory that provides more power. Objects are created when the configuration file is loaded

3.IOC operation Bean management

  1. What is Bean management
  • Create an object

  • Injection properties

  1. Beans manage operations in two ways
  • Based on XML configuration files
<! Create a people object -->
    <bean id="people" class="com.zhu.pojo.People"></bean>
Copy the code

(1) In the Spring configuration file, use the bean tag, add the corresponding attributes to the tag to create the object.

(2) Bean tag common attributes: ID attribute: unique identifier **class attribute: **class full path

(3) The default constructor is executed when creating an object

3. Inject attributes based on XMl

(1)DI: dependency injection

The first method uses the set method for injection.

public class Book {
    private String Bname;
    private String Bauthor;

    public void setBname(String bname) {
        Bname = bname;
    }
    public void setBauthor(String bauthor) {
        Bauthor = bauthor;
    }
    public void test(a){
        System.out.println(Bname+""+Bauthor); }}Copy the code
<bean id="book" class="com.zhu.pojo.Book">
        <!--  使用property完成属性注入      -->
        <property name="bname" value="Zhaohuaxishi"/>
        <property name="bauthor" value="Lu xun"/>
</bean>
Copy the code
@Test
public void testBook(a){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Book book = context.getBean("book", Book.class);
    System.out.println(book);
    book.test();
}
Copy the code

The second approach uses parameterized construction of injection.

<! -->
<bean id="orders" class="com.zhu.pojo.Orders">
    <constructor-arg name="oName" value="Computer"/>
    <constructor-arg name="address" value="China"/>
</bean>
Copy the code
public class Orders {
    private String oName;
    private String address;

    // There are parameters
    public Orders(String oName, String address) {
        this.oName = oName;
        this.address = address;
    }

    public void test(a) {
        System.out.println(oName + ""+ address); }}Copy the code
@Test
public void testOrders(a){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Orders orders = context.getBean("orders",Orders.class);
    System.out.println(orders);
    orders.test();
}
Copy the code
  • Annotation-based

4.IOC operation Bean management (XML injection of other attributes)

1. Literal

(1) the null value

<property name="bauthor">
    <null/>
</property>
Copy the code

2. The external Bean

The Service class invokes the DAO class

<bean id="userServiceImpl" class="com.zhu.service.UserServiceImpl">
        <! Insert the userDao object -->
        <property name="userdao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.zhu.Dao.UserDaoImpl"></bean>
Copy the code

3. Inside the bean

(1) One-to-many:

public class Emp {
    private String eName;
    private String gender;
    // The employee belongs to a department
    private Dept dept;
    
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public void seteName(String eName) {
        this.eName = eName;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void add(a){
        System.out.println(eName+""+gender+""+dept); }}Copy the code
public class Dept {
    private String dName;

    public void setdName(String dName) {
        this.dName = dName;
    }

    @Override
    public String toString(a) {
        return "Dept{" +
                "dName='" + dName + '\' ' +
                '} '; }}Copy the code
<bean id="emp" class="com.zhu.pojo.Emp">
    <property name="eName" value="Jack"/>
    <property name="gender" value="Male"/>
    <property name="dept">
        <bean id="dept" class="com.zhu.pojo.Dept">
            <property name="dName" value="Development"/>
        </bean>
    </property>
</bean>
Copy the code

4. Inject array, list, map, and set attributes

public class Stu {
    // Array type properties
    private String[] courses;
    // Collection type attributes
    private List<String> list;
    / / map types
    private Map<String,String> maps;
    private Set<String> set;

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public void test(a){ System.out.println(Arrays.toString(courses)); System.out.println(list); System.out.println(maps); System.out.println(set); }}Copy the code
<bean id="stu" class="com.zhu.pojo.Stu"> 
    <property name="courses" >
        <array>
            <value>Java development</value>
            <value>Linux operations</value>
        </array>
    </property>
    <property name="list">
        <list>
            <value>Zhang SAN</value>
            <value>Li si</value>
        </list>
    </property>
    <property name="maps">
        <map>
            <entry key="JAVA" value="java"></entry>
            <entry key="JSP" value="JSP"></entry>
        </map>
    </property>
    <property name="set">
        <set>
            <value>Mysql</value>
            <value>redis</value>
        </set>
    </property>
</bean>
Copy the code

Sets the object type value in the collection

<property name="courseList">
    <list>
        <ref bean="course1"></ref>
        <ref bean="course2"></ref>
    </list>
</property>
Copy the code
<bean id="course1" class="com.zhu.pojo.Course">
    <property name="courses">
        <value>Spring5 course</value>
    </property>
</bean>
<bean id="course2" class="com.zhu.pojo.Course">
    <property name="courses">
        <value>Mybatis course</value>
    </property>
</bean>
Copy the code

5.IOC operation Bean management (Bean scope)

1. Bean instances created in Spring are singleton instances by default

2. Configure multiple instances

<bean id="book" class="com.zhu.pojo.Book" scope="prototype">
Copy the code

3. The difference between Singleton and Prototype

When the scope value is set to Singleton, the spring configuration file is loaded to create a singleton

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Copy the code

When the scope value is set to Prototype, the multi-instance object is created when the getBean method is called

Book book1= context.getBean("book", Book.class);
Copy the code

6.IOC operation Bean Management (Bean life cycle)

1. Create bean instance with constructor (no parameter constructor)

2. Set the value for the bean properties (call the set method)

3. The rear pass bean instance bean postProcessBeforeInitialization processor

4. Call the bean’s initialization method (configuration required)

6. Bean can be used (object gets it)

7. When the container is closed, call the bean’s destruction method (configuration destruction method required)

<bean id="book" class="com.zhu.pojo.Book" init-method="initMethod" destroy-method="destroy">
Copy the code
package com.zhu.pojo;

public class Book {
    private String Bname;
    private String Bauthor;

    public Book(a) {
        System.out.println("Step 1: Create bean instance by performing no-argument construction");
    }

    public void setBname(String bname) {
        Bname = bname;
        System.out.println("The second part calls the set method to set the value of the property.");
    }

    public void setBauthor(String bauthor) {
        Bauthor = bauthor;
    }

    public void test(a) {
        System.out.println(Bname + "" + Bauthor);
    }

    public void initMethod(a){
        System.out.println("Step 3 Call the initialization method");
    }
    public void destroy(a){
        System.out.println("Step 5 Execution of destruction method"); }}Copy the code
@Test
public void testBook(a){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Book book1= context.getBean("book", Book.class);
    System.out.println("Step 4 Get the bean instance object");
    book1.test();
    context.close();
}
Copy the code
The first step: execute the no-parameter construction to create the bean instance. The second step is to call the set method to set the value of the property. The third step is to call the initialization methodnullStep 5 Execute the destruction method Process finished with exit code0

Copy the code