天天看點

spring架構的學習-DI依賴注入依賴注入的概念IOC和DI的意義

依賴注入的概念

  • 給屬性指派,就是依賴注入。

主要包括以下幾種指派方式

  1. 給基本屬性指派
  2. 給引用類型指派
  3. 給List,Map,Set,Properties指派

Xml配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="person" class="com.lzl.test.di.Person">
<!--
    property表示對象的屬性
     long或者string類型等基本類型,用value指派
       引用類型用ref指派
 -->
<property name="puId" value="5"></property>
<property name="pname" value="lisi"></property>
<property name="stu">
    <ref bean="stu"/>
</property>
<property name="plist">
    <list>
        <value>h1</value>
        <value>h2</value>
        <ref bean="stu"></ref>
    </list>
</property>
<property name="pmap">
    <map>
        <entry key="map1">
            <value>hhh</value>
        </entry>
        <entry key="map2">
            <value>xxx</value>
        </entry>
        <entry key="map3">
            <ref bean="stu"/>
        </entry>
    </map>
</property>
<property name="properties">
    <props>
        <prop key="pro1">899</prop>
        <prop key="pro2">444</prop>
        <prop key="pro3">8555</prop>
    </props>
</property>
</bean>
    <bean id="stu" class="com.lzl.test.di.Student"></bean>
</beans>
           

Java類

Person類

/**
 * 
 * @author lzl
 * @since 5.0
 */
public class Person {
private Long puId;
private String pname;
private Student stu;
private List plist;
private Map pmap;
private Properties properties;


public List getPlist() {
    return plist;
}

public void setPlist(List plist) {
    this.plist = plist;
}

public Map getPmap() {
    return pmap;
}

public void setPmap(Map pmap) {
    this.pmap = pmap;
}

public Properties getProperties() {
    return properties;
}

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

public Long getPuId() {
    return puId;
}

public void setPuId(Long puId) {
    this.puId = puId;
}

public String getPname() {
    return pname;
}

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

public Student getStu() {
    return stu;
}

public void setStu(Student stu) {
    this.stu = stu;
}

@Override
public String toString() {
    return "Person [puId=" + puId + ", pname=" + pname + ", stu=" + stu + "]";
}
}
           

Student類

public class Student {
public void sayStudent(){
    System.out.println("=我不是一名學生了!=");
}
}
           

測試類

public class DependenceInTest extends SpringHelper{
static{
    path = "org/springframework/jmx/helloWorldForDI.xml";
}
@Test
public void createBean(){
    Person p = (Person) this.fileResource.getBean("person");
    System.out.println(p.toString());
    p.getStu().sayStudent();
    System.out.println(p.getPlist());
    System.out.println(p.getPmap());
    System.out.println(p.getProperties());
}
}
           

IOC和DI的意義

  1. 建立對象
  2. 給對象屬性指派

Xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="person" class="com.lzl.test.diandioc.Person">
<!--
     constructor-arg給構造函數指派
     index 傳入參數的位置 ,從0開始
     type 參數的類型
     value 給基本類型指派
     ref 給引用類型指派
     注意
     隻能指定一個構造函數
 -->
<constructor-arg index="0" type="java.lang.String" value="hhh"></constructor-arg>
<constructor-arg index="1" ref="pstu"></constructor-arg>
</bean>
<bean id="pstu" class="com.lzl.test.di.Student"></bean>
</beans>
           

Person.java檔案

public class Person {
private Long pid;
private String pname;
private Student pstu;

public Person(String name,Student stu){
    this.pname = name;
    this.pstu = stu;
}

public Student getPstu() {
    return pstu;
}
public void setPstu(Student pstu) {
    this.pstu = pstu;
}

@Override
public String toString() {
    return "Person [pid=" + pid + ", pname=" + pname + ", pstu=" + pstu + "]";
}
}
           

Test檔案

public class DIAndIOCTest extends SpringHelper{
static{
    path="org/springframework/jmx/personBean.xml";
}
@Test
public void test(){
    Person p = (Person) this.fileResource.getBean("person");
    p.getPstu().sayStudent();
    System.out.println(p.toString());
}
}
           

面向接口程式設計

定義一個Document接口,定義一個read和write方法。

分别有WordDocument、PDFDocument、ExecleDocument實作這個接口。

并有DocumentManage類來管理這些實作類的建立。使用SpringIOC和DI來管理這些對象的建立。

Xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="documentManage" class="com.lzl.test.document.DocumentManage">
<!-- 
    document是一個接口
 -->
<property name="document">
<!-- 通過ref給接口指派 -->
    <ref bean="wordDocument"/>
</property>
</bean>
<bean id="wordDocument" class="com.lzl.test.document.WordDocument"></bean>
<bean id="pdfDocument" class="com.lzl.test.document.PDFDocument"></bean>
</beans>
           

Java類

Document接口

public interface Document {
void read();
void write();
}
           

實作類 這裡隻列舉一個。

public class WordDocument implements Document{

@Override
public void read() {
    System.out.println("Word Read");
}
@Override
public void write() {
    System.out.println("Word Write");
}
}
           

管理類

public class DocumentManage{
private Document document;

public void read(){
    this.document.read();
}
public Document getDocument() {
    return document;
}

public void setDocument(Document document) {
    this.document = document;
}
public void write(){
    this.document.write();
}
}
           

測試類

public class DocumentTest extends SpringHelper{

static{
    path="org/springframework/jmx/documentTest.xml";
}
@Test
public void test(){
    DocumentManage dm = (DocumentManage) this.fileResource.getBean("documentManage");
    dm.read();
}
}