Spring - IOC (inversion of control)

1, IOC container


1. What is IOC (control reversal)

(a) the creation of objects and the calling process between objects are handed over to Spring for management

(b) purpose of using IOC: to reduce coupling

2. IOC bottom layer

(a) parsing and reflection mode

3. The IOC container provided by Spring is implemented in two ways (two interfaces)

(a) BeanFactory interface: the basic implementation of IOC container is the use interface of Spring internal interface, which is not provided to developers for use (the object will not be created when loading the configuration file, but will be created when obtaining the object.)

(b) ApplicationContext interface: the sub interface of BeanFactory interface, which provides more and more powerful functions for developers to use (the object in the configuration file will be created when loading the configuration file). It is recommended to use!

4. Implementation class of ApplicationContext interface (see API document for details) ☺)

2, IOC container Bean management


1. IOC operation Bean management

Bean management is two operations:

(1) Spring creates objects;

(2) Spring injection attribute

2. Create objects based on XML configuration files

<!--1 to configure User objects creating-->
<bean id="user" class="com.atguigu.spring5.User"></bean>


3. Attribute injection based on XML (DI: dependency injection (injection attribute))

(a) set mode injection

//(1) Traditional methods: create classes, define attributes and corresponding set methods
public class Book {
        //Create attribute
        private String bname;

        //Create the set method corresponding to the attribute
        public void setBname(String bname) {
            this.bname = bname;
        }
   }
<!--(2)spring Method: set Method injection properties-->
<bean id="book" class="com.atguigu.spring5.Book">
    <!--use property Complete attribute injection
        name: Attribute name in class
        value: Values injected into attributes
    -->
    <property name="bname" value="Hello"></property>
    <property name="bauthor" value="World"></property>
</bean>


(b) parametric constructor injection

//(1) Traditional way: create classes and build parametric functions
public class Orders {
    //attribute
    private String oname;
    private String address;
    //Parametric construction
    public Orders(String oname,String address) {
        this.oname = oname;
        this.address = address;
    }
  }
<!--(2)spring Method: the injection attribute is constructed with parameters-->
<bean id="orders" class="com.atguigu.spring5.Orders">
    <constructor-arg name="oname" value="Hello"></constructor-arg>
    <constructor-arg name="address" value="China!"></constructor-arg>
</bean>


(c) p namespace injection (just understand)

<!--1,add to p The namespace is in the header of the configuration file-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p "<! -- add a line P -- >

<!--2,stay bean Tag for attribute injection set Mode injection (simplified operation)-->
    <bean id="book" class="com.atguigu.spring5.Book" p:bname="very" p:bauthor="good">
    </bean>


4. Inject null values and special symbols

<bean id="book" class="com.atguigu.spring5.Book">
    <!--(1)null value-->
    <property name="address">
        <null/><!--Add one in the attribute null label-->
    </property>
    
    <!--(2)Special symbol assignment-->
     <!--Attribute values contain special symbols
       a hold<>Escape &lt; &gt;
       b Write content with special symbols to CDATA
      -->
        <property name="address">
            <value><![CDATA[<<Nanjing>>]]></value>
        </property>
</bean>


5. Injection attribute - External bean

(a) create two classes: service and dao

public class UserService {//service class

    //Create UserDao type attribute and generate set method
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add() {
        System.out.println("service add...............");
        userDao.update();//Call dao method
    }
}

public class UserDaoImpl implements UserDao {//dao class

    @Override
    public void update() {
        System.out.println("dao update...........");
    }
}


(b) configure in the spring configuration file

<!--1 service and dao objects creating-->
<bean id="userService" class="com.atguigu.spring5.service.UserService">
    <!--injection userDao object
        name Attribute: the name of the attribute in the class
        ref Properties: Creating userDao object bean label id value
    -->
    <property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoImpl"></bean>


6. Inject internal bean s and cascade assignment based on XML

(a) injection attribute - internal bean

(1) One to many relationships: departments and employees
A department has multiple employees, and an employee belongs to a department (Department is one and employees are many)
(2) One to many relationships are represented between entity classes. Employees represent their departments and are represented by object type attributes

//Department category
public class Dept {
    private String dname;
    public void setDname(String dname) {
        this.dname = dname;
    }
}

//Employee category
public class Emp {
    private String ename;
    private String gender;
    //Employees belong to a department and are represented in the form of objects
    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;
    }
}


(3) Configure in spring configuration file

<!--inside bean-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--Set two common properties-->
        <property name="ename" value="Andy"></property>
        <property name="gender" value="female"></property>
        <!--Set object type properties-->
        <property name="dept">
            <bean id="dept" class="com.atguigu.spring5.bean.Dept"><!--inside bean assignment-->
                <property name="dname" value="Publicity Department"></property>
            </bean>
        </property>
    </bean>


(b) injection attribute - cascade assignment

<!--Method 1: cascade assignment-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--Set two common properties-->
        <property name="ename" value="Andy"></property>
        <property name="gender" value="female"></property>
        <!--Cascade assignment-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring5.bean.Dept">
        <property name="dname" value="departments of public relations"></property>
    </bean>
 //Method 2: generate get method of dept (get method must have!!)
    public Dept getDept() {
        return dept;
    }

 

 <!--Cascade assignment-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--Set two common properties-->
        <property name="ename" value="jams"></property>
        <property name="gender" value="male"></property>
        <!--Cascade assignment-->
        <property name="dept" ref="dept"></property>
        <property name="dept.dname" value="Technical department"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring5.bean.Dept">
    </bean>


7. IOC operation Bean management - xml injection set attribute

1. Inject array type attribute

2. Inject List collection type attribute

3. Inject Map set type attribute

//(1) Create classes, define array, list, map and set type attributes, and generate corresponding set methods
public class Stu {
    //1 array type properties
    private String[] courses;
    //2 list collection type attribute
    private List<String> list;
    //3 map collection type attribute
    private Map<String,String> maps;
    //4 set set type attribute
    private Set<String> sets;
    
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    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;
    }

 

<!--(2)stay spring Configuration file-->
    <bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">
        <!--Array type attribute injection-->
        <property name="courses">
            <array>
                <value>java curriculum</value>
                <value>Database course</value>
            </array>
        </property>
        <!--list Type attribute injection-->
        <property name="list">
            <list>
                <value>Zhang San</value>
                <value>the other woman</value>
            </list>
        </property>
        <!--map Type attribute injection-->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java"></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>
        <!--set Type attribute injection-->
        <property name="sets">
            <set>
                <value>MySQL</value>
                <value>Redis</value>
            </set>
        </property>
</bean>


8. Set the object type value in the collection

  //Students learn many courses
    private List<Course> courseList;//Create collection
    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

 

  <!--Create multiple course object-->
    <bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="Spring5 frame"></property>
    </bean>
    <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="MyBatis frame"></property>
    </bean>
    
       <!--injection list Collection type, value is object-->
       <property name="courseList">
           <list>
               <ref bean="course1"></ref>
               <ref bean="course2"></ref>
           </list>
       </property>
<!--Step 1: in spring Introducing namespaces into configuration files util-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework. Org / schema / util "<! -- add util namespace -- >
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework. org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd "> <! -- add util namespace -- >
    
<!--Step 2: use util Label complete list Set injection extraction-->
<!--Extract the set injection part-->
 <!--1 extract list Collection type attribute injection-->
    <util:list id="bookList">
        <value>Yi Jin Jing</value>
        <value>The nine Yin manual</value>
        <value>nine men's power</value>
    </util:list>

 <!--2 extract list Collection type attribute injection usage-->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>

 

Keywords: Java Spring xml ioc

Added by Rigo on Thu, 10 Feb 2022 12:19:11 +0200