1. 程式人生 > >Spring初學三(對spring作為容器的理解)

Spring初學三(對spring作為容器的理解)

以前,一直不清楚spring容器的概念,現在終於理解了。

spring作為容器,其實最重要的就是配置檔案xml。在裡面建立好各種各樣的bean,有屬性注入的(注入字串、list、map、set等集合)、函式注入、物件注入(將一個bean通過ref引用注入到)等,這一步只是建立好了bean,但是還沒有放到容器中,要將其放到容器中才可以使用。容器有兩種,分別是BeanFactory、上下文ApplicationContext。放入容器,就是將配置檔案xml載入進來,具體載入方式如下表,這個時候並沒有建立bean例項,在getBean時才真正建立了bean例項。

BeanFactory方式

主要是用XmlBeanFactory

載入方法有多種,分別是

org.springframework.core.io.ByteArrayResource 位元組給定的內容
org.springframework.core.io.ClassPathResource 從classpath提取資源
org.springframework.core.io.DescriptiveResource 資源描述符
org.springframework.core.io.FileSystemResource 從檔案系統提取資源
org.springframework.core.io.InputStreamResource 從輸入流提取資源
org.springframework.web.context.support.ServletContextResource 從servlet上下文中取資源
org.springframework.core.io.UrlResource 從給定的url中取資源

具體使用如下(以檔案系統提取舉例子):

 BeanFactory factory=new XmlBeanFactory(new FileSystemResource("c:/beans.xml"));

ApplicationContext方式

載入方法主要有三種,如下

org.springframework.context.support.ClassPathXmlApplicationContext 從類路徑中的xml檔案載入bean資源
org.springframework.context.support.FileSystemXmlApplicationContext 從檔案系統中的xml檔案載入bean資源
org.springframework.web.context.WebApplicationContext 從web系統中的xml中載入Bean資源
具體使用如下(以類路徑載入舉例子)

 ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/springinaction/springidol/spring-idol.xml");
   

下面通過幾個例子來加強下理解

1.配置bean(各種注入如下)

 <bean id="baseSaxophonist"
      class="com.springinaction.springidol.Instrumentalist"
      abstract="true">
    <property name="song" value="Jingle Bells" />                                       //注入字串
    <property name="instrument" ref="saxophone" />                                  //注入物件

  </bean>

<bean id="hank" class="com.springinaction.springidol.OneManBand">
    <property name="instruments">
      <!--
      <list>                                                                                                           //注入list
          <ref bean="guitar" />
          <ref bean="saxophone" />
          <ref bean="cymbal" />
          <ref bean="cymbal" />
      </list>
      -->
      <!--
      <props>                                                                                                       //注入props
        <prop key="GUITAR">STRUM STRUM STRUM</prop>
        <prop key="CYMBAL">CRASH CRASH CRASH</prop>
        <prop key="HARMONICA">HUM HUM HUM</prop>
      </props>
      -->
      <map>                                                                                                        //注入map
        <entry key="GUITAR" value="STRUM STRUM STRUM" />
      </map>
    </property>
  </bean>

<bean id="duke"
      class="com.springinaction.springidol.PoeticJuggler"
      autowire="constructor">                                                                        //自動注入,byname、bytype、by建構函式constructor、autodetect
<!--     <constructor-arg ref="sonnet29" />  -->
  </bean>


2.將bean放到容器中

 ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "com/springinaction/springidol/spring-idol.xml");

3.例項化bean

TalentCompetition competition =   (TalentCompetition) ctx.getBean("springIdol");

通過反射機制,可以使用bean對應class裡面的方法。