Spring入門學習手冊 1:最簡單的反轉控制

EatherToo發表於2019-01-24

一、什麼是Javabean

看到的一個比較專業的解釋是:

JavaBean定義了一組規則

JavaBean就是遵循此規則的平常的Java物件

JavaBean是一種特殊的Java類,提供getter 和 setter方法訪問它的屬性。具體的內容可以檢視:

菜鳥教程Javabean

學習JavaBean

JavaBean可以跨平臺,可以用於多種情況下。

二、Spring裡面怎麼用JavaBean

首先定義一個JavaBean:

package com.learn.springDemo;

public class Category {
    private int id;
    private String name;

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

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

    public void setName(String name) {
        this.name = name;
    }
}
複製程式碼

然後建立一個xml檔案,基於這個檔案來配置JavaBean

<?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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>

</beans>
複製程式碼

可以看到xml中配置了JavaBean的屬性。 當然,還有基於註解來配置的方式使用 Java 配置進行 Spring bean 管理

使用時直接從容器中取出來就行:

package com.learn.springTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.springDemo.Category;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

        Category c =  (Category) ac.getBean("c");

        System.out.println(c.getName() + " " + c.getId());

    }

}
複製程式碼

執行結果:

Eather 12345
複製程式碼

三、一些問題

  • 每次取出的JavaBean是一個嗎?
package com.learn.springTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.springDemo.Category;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});

        Category c =  (Category) ac.getBean("c");

        System.out.println(c.getName() + " " + c.getId());

        c.setId(10086);
        c.setName("David");

/////////////////////////////////////////////////////
        Category cc = (Category) ac.getBean("c");//再取一個取出來
        System.out.println(cc.getName() + " " + cc.getId());//列印


    }

}
複製程式碼

根據這段程式碼的執行結果,我認為應該是同一個,每次取出的或者說引用的都是同一個物件,在程式開始後的某一個時刻初始化好後就一直用的是一個物件。

Eather 12345
David 10086

複製程式碼
  • JavaBean是什麼時候被初始化的?

    看看下面這段程式碼

    package com.learn.springTest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.learn.springDemo.Category;
    public class Test {
      public static void main(String args[]) {
          ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
    
          Category c = (Category) ac.getBean("c");
    
          System.out.println(c.getName() + " " + c.getId());
    
          c.setId(10086);
          c.setName("David");
    
          //重新初始化一個ApplicationContext
          ApplicationContext ac2 = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
          Category cc = (Category) ac2.getBean("c");//再取一個取出來
          System.out.println(cc.getName() + " " + cc.getId());//列印
    
    
          }
      }
    複製程式碼

    執行結果為:

    Eather 12345
    Eather 12345
    複製程式碼

    JavaBean是什麼時候初始化就很顯而易見了。

  • 依賴注入怎麼搞(在一個類裡面引用另一個類的例項

<?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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>
        <!--這裡注入Category例項,已經初始化過的-->
        <property name="category" ref="c"></property>
    </bean>

</beans>
複製程式碼

Product類的定義是這樣的:

package com.learn.springDemo;

public class Product {
    private int id;
    private String name;
    private Category category;

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

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

    public void setCategory(Category category) {
        this.category = category;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }
}
複製程式碼

顯然,在依賴注入時,xml文件中要用 ref,而非value

此外,依賴注入的必須是例項化好的物件。

  • xml裡面可以寫多個相同的bean嗎?
<?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 name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="ccc" class="com.learn.springDemo.Category">
        <property name="name" value="Tom"></property>
        <property name="id" value="111111"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>
        <!--這裡注入Category例項,已經初始化過的-->
        <property name="category" ref="c"></property>
    </bean>

</beans>
複製程式碼

非常顯然可以,只要name不相同就可以(這個幾乎是廢話)。

相關文章