天天看點

Java練習(七):Spring應用之DI (簡單的StudentDI工程)1. 背景概念2. 簡單的StudentDI工程,展示了常見的注入方式的使用(配置)補充:Eclipse中,一個快速生成getter和setter方法的小技巧

目錄

1. 背景概念

1.1 DI定義

1.2 常見注入方式的xml配置

2. 簡單的StudentDI工程,展示了常見的注入方式的使用(配置)

2.1 maven系統的配置檔案:pom.xml

2.2 bean的配置檔案: bean_di.xml

2.3 類代碼:StudentDI.java , Address.java (該類被StudentDI引用)

2.4 測試類

補充:Eclipse中,一個快速生成getter和setter方法的小技巧

1. 背景概念

1.1 DI定義

(1) IoC的一個重點是:在系統運作中,動态的向某個對象提供它所需要的其他對象。這一點是通過DI來實作的。

(2)DI, 依賴注入:用一個單獨的對象(裝配器)來裝配對象之間的依賴關系。DI,即元件之間的依賴關系由容器在運作期間決定,即由容器動态的将某個依賴關系注入到元件中。

(3)舉例:

對象A要操作資料庫,以前我們要在A中自己編寫代碼來獲得一個Connection對象,有了Spring後我們就隻需要告訴Spring, A中需要一個Connection. 至于怎麼構造這個connection?何時構造?A不需要知道。這樣就完成了對各個對象之間關系的控制。

(4)DI如何實作?通過反射。它允許程式在運作的時候動态的生成對象,執行對象的方法,改變對象的屬性,Spring就是通過反射來實作注入的。

1.2 常見注入方式的xml配置

bean注入

數組注入

List注入

Map注入

set注入

null注入

Properties注入

2. 簡單的StudentDI工程,展示了常見的注入方式的使用(配置)

Java練習(七):Spring應用之DI (簡單的StudentDI工程)1. 背景概念2. 簡單的StudentDI工程,展示了常見的注入方式的使用(配置)補充:Eclipse中,一個快速生成getter和setter方法的小技巧

2.1 maven系統的配置檔案:pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.my.test</groupId>

  <artifactId>SpringDIDemo</artifactId>

  <version>0.0.1-SNAPSHOT</version>

   <dependencies>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>5.3.3</version>

        </dependency>        

        <dependency>

            <groupId>org.aspectj</groupId>

            <artifactId>aspectjweaver</artifactId>

            <version>1.9.4</version>

        </dependency>

    </dependencies>

</project>

2.2 bean的配置檔案: bean_di.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"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:c="http://www.springframework.org/schema/c"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

        https://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context

        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />

    <bean id="addr" class="com.my.test.Address">

        <property name="address" value="上海" />

    </bean>

    <bean id="student1" class="com.my.test.StudentDI">

        <property name="name" value="Sheryl" />

        <property name="address" ref="addr" />

        <property name="books">

            <array>

                <value>數學</value>

                <value>國文</value>

                <value>英語</value>

            </array>

        </property>

        <property name="hobbys">

            <list>

                <value>聽歌</value>

                <value>看電影</value>

                <value>逛街</value>

            </list>

        </property>

        <property name="card">

            <map>

                <entry key="招行" value="123456789" />

                <entry key="工行" value="987654321" />

            </map>

        </property>

        <property name="games">

            <set>

                <value>CS</value>

                <value>鬥地主</value>

                <value>高爾夫</value>

            </set>

        </property>

        <property name="wife">

            <null />

        </property>

        <property name="info">

            <props>

                <prop key="學号">12345678</prop>

                <prop key="性别">女</prop>

                <prop key="姓名">Sheryl</prop>

            </props>

        </property>

    </bean>

</beans>

2.3 類代碼:StudentDI.java , Address.java (該類被StudentDI引用)

(1)StudentDI.java

package com.my.test;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

import com.my.test.Address;

public class StudentDI {

    private String name;

    private Address address;

    private String[] books;

    private List<String> hobbys;

    private Map<String,String> card;

    private Set<String> games;

    private String wife;

    private Properties info;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public Address getAddress() {

        return address;

    }

    public void setAddress(Address address) {

        this.address = address;

    }

    public String[] getBooks() {

        return books;

    }

    public void setBooks(String[] books) {

        this.books = books;

    }

    public List<String> getHobbys() {

        return hobbys;

    }

    public void setHobbys(List<String> hobbys) {

        this.hobbys = hobbys;

    }

    public Map<String, String> getCard() {

        return card;

    }

    public void setCard(Map<String, String> card) {

        this.card = card;

    }

    public Set<String> getGames() {

        return games;

    }

    public void setGames(Set<String> games) {

        this.games = games;

    }

    public String getWife() {

        return wife;

    }

    public void setWife(String wife) {

        this.wife = wife;

    }

    public Properties getInfo() {

        return info;

    }

    public void setInfo(Properties info) {

        this.info = info;

    }

    public void showStudentInfo(){

        System.out.println("name="+ name+ ",address="+ address.getAddress()+ ",books=");

        for (String book:books){

            System.out.print("<<"+book+">>\t");

        }

        System.out.println("\nhobbys:"+hobbys);

        System.out.println("card:"+card);

        System.out.println("games:"+games);

        System.out.println("wife:"+wife);

        System.out.println("info:"+info);

    }   

}

(2)Address.java

package com.my.test;

public class Address {

    private String address;

    public String getAddress() {

        return address;

    }

    public void setAddress(String address) {

        this.address = address;

    }

}

2.4 測試類

package com.my.test.demo;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.my.test.StudentDI;

public class StudentDITest {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("bean_di.xml");

        StudentDI studentDI = (StudentDI) context.getBean("student1");

        studentDI.showStudentInfo();

    }

}

輸出結果:

name=Sheryl,address=上海,books=

<<數學>>    <<國文>>    <<英語>>    

hobbys:[聽歌, 看電影, 逛街]

card:{招行=123456789, 工行=987654321}

games:[CS, 鬥地主, 高爾夫]

wife:null

info:{學号=12345678, 性别=女, 姓名=Sheryl}

補充:Eclipse中,一個快速生成getter和setter方法的小技巧

Java練習(七):Spring應用之DI (簡單的StudentDI工程)1. 背景概念2. 簡單的StudentDI工程,展示了常見的注入方式的使用(配置)補充:Eclipse中,一個快速生成getter和setter方法的小技巧
Java練習(七):Spring應用之DI (簡單的StudentDI工程)1. 背景概念2. 簡單的StudentDI工程,展示了常見的注入方式的使用(配置)補充:Eclipse中,一個快速生成getter和setter方法的小技巧