003. Spring 依賴注入

1、創建Java項目:File -> New -> Java Project

2、引入必要jar包,項目結構如下
這裏寫圖片描述

3、創建People實體類People.java

package com.spring.model;

public class People {

    private int id;
    private String name;

    public People() {
        super();
    }

    public People(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + "]";
    }

}

4、創建PeopleFactory工廠類PeopleFactory.java

package com.spring.factory;

import com.spring.model.People;

public class PeopleFactory {

    public People newPeople() {
        return new People(5, "週五");
    }

}

5、創建PeopleStaticFactory靜態工廠類PeopleStaticFactory.java

package com.spring.factory;

import com.spring.model.People;

public class PeopleStaticFactory {

    public static People newPeople() {
        return new People(6, "吳六");
    }

}

6、創建spring配置文件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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="people" class="com.spring.model.People"></bean>

    <!-- 屬性注入 -->
    <bean id="people1" class="com.spring.model.People">
        <property name="id" value="1"></property>
        <property name="name" value="趙一"></property>
    </bean>

    <!-- 構造方法注入 -->

    <!-- 通過類型-->
    <bean id="people2" class="com.spring.model.People">
        <constructor-arg type="int" value="2"></constructor-arg>
        <constructor-arg type="String" value="錢二"></constructor-arg>
    </bean>
    <!-- 通過索引-->
    <bean id="people3" class="com.spring.model.People">
        <constructor-arg index="0" value="3"></constructor-arg>
        <constructor-arg index="1" value="孫三"></constructor-arg>
    </bean>
    <!-- 聯合使用-->
    <bean id="people4" class="com.spring.model.People">
        <constructor-arg index="0" type="int" value="4"></constructor-arg>
        <constructor-arg index="1" type="String" value="李四"></constructor-arg>
    </bean>

    <!-- 構造方法注入 END -->    

    <!--工廠方法注入-->
    <bean id="peopleFactory" class="com.spring.factory.PeopleFactory"></bean>
    <bean id="people5" factory-bean="peopleFactory" factory-method="newPeople"></bean>

    <!--靜態工廠方法注入-->
    <bean id="people6" class="com.spring.factory.PeopleStaticFactory" factory-method="newPeople"></bean>

</beans>

7、創建Spring測試類SpringUnit.java

package com.spring.junit;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.model.People;

public class SpringUnit {

    @Test
    public void test() {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        People people = null;

        people = (People) ctx.getBean("people");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people1");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people2");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people3");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people4");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people5");
        System.out.println(people.toString());

        people = (People) ctx.getBean("people6");
        System.out.println(people.toString());

        ctx.close();
    }

}

8、測試結果

... 省略Spring日誌信息 ...

People [id=0, name=null]
People [id=1, name=趙一]
People [id=2, name=錢二]
People [id=3, name=孫三]
People [id=4, name=李四]
People [id=5, name=週五]
People [id=6, name=吳六]

... 省略Spring日誌信息 ...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章