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

1、什麼是Javabean

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

JavaBean定義了一組規則

JavaBean就是遵循此規則的日常的Java對象

JavaBean是一種特殊的Java類,提供getter 和 setter方法訪問它的屬性。具體的內容能夠查看:html

菜鳥教程Javabeanjava

學習JavaBeanweb

JavaBean能夠跨平臺,能夠用於多種狀況下。spring

2、Spring裏面怎麼用JavaBean

首先定義一個JavaBean:bash

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文件,基於這個文件來配置JavaBeanapp

<?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 管理jsp

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

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());

    }

}
複製代碼

運行結果:this

Eather 12345
複製代碼

3、一些問題

  • 每次取出的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不相同就能夠(這個幾乎是廢話)。

相關文章
相關標籤/搜索