Spring第四章:IOP/DI

1.如何給 Bean 的屬性賦值(注入)

  1.1經過構造方法設置值.

    1.1.1在 applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    

    <!--
        注意 在使用構造方法的時候一旦寫了一個有參的構造方法那麼就會覆蓋掉本來無參的構造方法
        須要手動的添加無參構造器 不然會出現錯誤  No default constructor found
       index  表明參數的位置  從0開始計算
       type   指的是參數的類型,在有多個構造函數時,能夠用type來區分,要是能肯定是那個構造函數,能夠不用寫type
       value  給基本類型賦值
       ref    給引用類型賦值
     -->
    <bean id="person_con" class="com.suncl.model.People">
        <constructor-arg index="0" type="java.lang.Long" value="1">
        </constructor-arg>
        <constructor-arg index="1" type="com.suncl.model.Student" ref="student_con"></constructor-arg>
    </bean>
    <bean id="student_con" class="com.suncl.model.Student">
        <constructor-arg index="0" type="java.lang.String" value="sName"></constructor-arg>
    </bean>

</beans>

 

 

 

    1.1.2實體類

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class Student {

    private String name;

    /**
     * 注意 在使用構造方法的時候一旦寫了一個有參的構造方法那麼就會覆蓋掉本來無參的構造方法
     須要手動的添加無參構造器 不然會出現錯誤  No default constructor found
     */
    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }


}

 

package com.suncl.model;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by SCL-PC on 2019/2/26.
 */
public class People {

    private Long pid;
    private String pname;
    private Student students;
    private List lists;
    private Set sets;
    private Map maps;
    private Properties properties;


    /**
     * 注意 在使用構造方法的時候一旦寫了一個有參的構造方法那麼就會覆蓋掉本來無參的構造方法
     須要手動的添加無參構造器 不然會出現錯誤  No default constructor found
     */
    public People(){

    }

    /**
     * 有參構造器
     * @param pid
     * @param students
     */
    public People(Long pid, Student students) {
        this.pid = pid;
        this.students = students;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Student getStudents() {
        return students;
    }

    public void setStudents(Student students) {
        this.students = students;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Set getSets() {
        return sets;
    }

    public void setSets(Set sets) {
        this.sets = sets;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }



    @Override
    public String toString() {
        return "People{" +
                "pid=" + pid +
                ", pname='" + pname + '\'' +
                ", students=" + students +
                ", lists=" + lists +
                ", sets=" + sets +
                ", maps=" + maps +
                ", properties=" + properties +
                '}';
    }
}

 

 

    1.1.3測試類

 

package com.suncl.test;

import com.suncl.model.People;
import com.suncl.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class TestDI {

    public static void main(String[] args) {

        ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");



        /**
         * 構造器注入
         */
        People peopleCon = actx.getBean("person_con",People.class);
        System.out.println(peopleCon.toString());
        Student studentCon = actx.getBean("student_con",Student.class);
        System.out.println(studentCon.toString());
    }

}

 

 

 

    1.1.4運行結果

 

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People{pid=1, pname='null', students=Student{name='sName'}, lists=null, sets=null, maps=null, properties=null}
Student{name='sName'}

 

 

 

  1.2設置注入(經過 set 方法)

    1.2.1applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="people" class="com.suncl.model.People" >
        <!--set方式注入Long類型-->
        <property name="pid" value="1"></property>
        <!--set方式注入String類型-->
        <property name="pname" value="suncl"></property>
        <!--set方式注入對象-->
        <property name="students">
            <ref bean="student"/>
        </property>
        <!--set方式注入 List 三個元素-->
        <property name="lists">
            <list>
                <value>1</value>
                <ref bean="student"/>
                <value>vae1</value>
            </list>
        </property>
        <!--set方式注入 Set 三個元素 -->
        <property name="sets">
            <set>
                <value>1</value>
                <ref bean="student"/>
                <value>vae</value>
            </set>
        </property>

        <!--set方式注入 Map 2個元素 -->
        <property name="maps">
            <map>
                <entry key="m1" value="1"></entry>
                <entry key="m2" >
                    <ref bean="student" />
                </entry>
            </map>
        </property>

        <!--set方式注入 Properties 2個元素 -->
        <property name="properties">
            <props>
                <prop key="p1">p1</prop>
                <prop key="p2">p2</prop>
            </props>
        </property>
    </bean>

    <!--使用set方式注入-->
    <bean id="student" class="com.suncl.model.Student">
        <property name="name" value="sName"></property>
    </bean>


</beans>

 

 

 

 

    1.2.2實體類

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class Student {

    private String name;

    /**
     * 注意 在使用構造方法的時候一旦寫了一個有參的構造方法那麼就會覆蓋掉本來無參的構造方法
     須要手動的添加無參構造器 不然會出現錯誤  No default constructor found
     */
    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }


}

 

package com.suncl.model;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by SCL-PC on 2019/2/26.
 */
public class People {

    private Long pid;
    private String pname;
    private Student students;
    private List lists;
    private Set sets;
    private Map maps;
    private Properties properties;


    /**
     * 注意 在使用構造方法的時候一旦寫了一個有參的構造方法那麼就會覆蓋掉本來無參的構造方法
     須要手動的添加無參構造器 不然會出現錯誤  No default constructor found
     */
    public People(){

    }

    /**
     * 有參構造器
     * @param pid
     * @param students
     */
    public People(Long pid, Student students) {
        this.pid = pid;
        this.students = students;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public Student getStudents() {
        return students;
    }

    public void setStudents(Student students) {
        this.students = students;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Set getSets() {
        return sets;
    }

    public void setSets(Set sets) {
        this.sets = sets;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }



    @Override
    public String toString() {
        return "People{" +
                "pid=" + pid +
                ", pname='" + pname + '\'' +
                ", students=" + students +
                ", lists=" + lists +
                ", sets=" + sets +
                ", maps=" + maps +
                ", properties=" + properties +
                '}';
    }
}

 

 

 

    1.2.3測試類

 

package com.suncl.test;

import com.suncl.model.People;
import com.suncl.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class TestDI {

    public static void main(String[] args) {

        ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");

        /**
         * set方式注入
         */
        People people = actx.getBean("people",People.class);
        System.out.println(people.toString());
        Student student = actx.getBean("student",Student.class);
        System.out.println(student.toString());


    }

}

 

 

 

 

    1.2.4運行結果

 

 

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People{pid=1, pname='suncl', students=Student{name='sName'}, lists=[1, Student{name='sName'}, vae1], sets=[1, Student{name='sName'}, vae], maps={m1=1, m2=Student{name='sName'}}, properties={p2=p2, p1=p1}}
Student{name='sName'}

 

 

  1.3 DI

    1.3.1. DI:中文名稱:依賴注入

    1.3.2. 英文名稱((Dependency Injection)

    1.3.3. DI 是什麼?

      1.3.3.1 DI IoC 是同樣的

      1.3.3.2 當一個類(A)中須要依賴另外一個類()對象時,B 賦值給 A 的過程就叫作依賴注入

      1.3.3.3 代碼體現

        1.3.3.3.1 applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--依賴注入 DI-->
    <bean id="student" class="com.suncl.model.Student" >
        <property name="name" value="zhangsan"></property>
    </bean>

    <bean id="school" class="com.suncl.model.School">
        <property name="student" ref="student"></property>
    </bean>

</beans>

 

 

 

 

        1.3.3.3.2 實體類

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class School {

    private Student student;


    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "School{" +
                "student=" + student +
                '}';
    }
}

 

package com.suncl.model;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class Student {

    private String name;

    /**
     * 注意 在使用構造方法的時候一旦寫了一個有參的構造方法那麼就會覆蓋掉本來無參的構造方法
     須要手動的添加無參構造器 不然會出現錯誤  No default constructor found
     */
    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }


}

 

 

 

        1.3.3.3.3測試類

 

package com.suncl.test;

import com.suncl.model.People;
import com.suncl.model.School;
import com.suncl.model.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by SCL-PC on 2019/2/27.
 */
public class TestDI {

    public static void main(String[] args) {

        ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");


        /**
         * 依賴注入 建立學校的時候默認注入了一個學生
         */
        School school = actx.getBean("school",School.class);
        System.out.println(school.toString());

    }

}

 

 

 

 

        1.3.3.3.4運行結果

 

二月 27, 2019 11:45:34 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
School{student=Student{name='zhangsan'}}

 

以上代碼放在 :java

連接:https://pan.baidu.com/s/1iiZFoVDYOMY7Zd-kXksdgw
提取碼:txx9

spring

相關文章
相關標籤/搜索