spring2——IOC之Bean的裝配

來源:互聯網
上載者:User

標籤:標籤   技術分享   java   資料   strong   1.0   執行個體   mvc   port   

  spring容器對於bean的裝配提供了兩個介面容器分別是"ApplicationContext介面容器"和"BeanFactory介面容器",其中"BeanFactory介面容器"是spring的頂級介面容器,"ApplicationContext介面容器"繼承了"BeanFactory介面容器",並且兩個介面容器對於Bean的裝配時機是不同的,ApplicationContext介面容器是在容器初始化階段就對容器中所有的bean進行了裝配,而BeanFactory介面容器介面容器是在容器物件調用getBean方法是才對bean進行裝配的。

  另外,spring的這兩種介面容器在對bean進行裝配時,都是先去調用類的無參構造方法,建立一個空值對象。

  下面我們通過執行個體來進行說明。

  添加maven依賴。

<properties>        <org.springframework.version>4.1.4.RELEASE</org.springframework.version>    </properties>    <dependencies>        <!-- spring相關jar -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>${org.springframework.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${org.springframework.version}</version>        </dependency>    </dependencies>

 

Student類

public class Student {    public Student() {        System.out.println("spring載入String類");    }    }

設定檔:

<?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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd                    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd                    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <bean id="student" class="com.opensource.bean.Student"></bean>    </beans>

測試類別:

public class MyTest {    public static void main(String[] args) {                ApplicationContext ac = new ClassPathXmlApplicationContext("spring-bean.xml");                BeanFactory bf= new XmlBeanFactory(new ClassPathResource("spring-bean.xml"));        bf.getBean("student");    }}

我們分別來看一下兩個容器對於建立Student類的執行個體時的日誌:

(1)ApplicationContext介面容器

(2) BeanFactory介面容器,只有在調用getBean("student")方法時才會調用Student的無參構造方法,在容器中建立Studnet的執行個體。

調用getBean("student");

  在這裡,我們可以狹義的把spring的設定檔看作是spring容器的顯式表現,因為在設定檔中我們可以看到容器對那些bean進行了裝配。從容器的頂級介面BeanFactory我們不難看出,spring容器對於bean的建立使用了原廠模式,從<bean id="student" class="com.opensource.bean.Student"></bean>標籤的class屬性使用了全類名,我們也可以知道對於執行個體的建立使用了java的反射機制。id屬性具有唯一性,我們從容器中取得執行個體使用了getBean("student"),也可以看出來spring容器對於本的裝配使用的資料結構應該是Map集合的資料形式。

  最後說一點,我們作為程式員,研究問題還是要仔細深入一點的。當你對原理瞭解的有夠透徹,開發起來也就得心應手了,很多開發中的問題和疑惑也就迎刃而解了,而且在面對其他問題的時候也可做到觸類旁通。當然在開發中沒有太多的時間讓你去研究原理,開發中要以實現功能為前提,可等項目上線的後,你有大把的時間或者空餘的時間,你大可去刨根問底,深入的去研究一項技術,為覺得這對一名程式員的成長是很重要的事情。

 

spring2——IOC之Bean的裝配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.