Spring基礎 ----IOC(1)

Spring-IOC

spring-ico(控制反轉) :把對象的創建,初始化,銷燬等工作交給Spring容器來執行
由Spring容器控制對象的生命週期

對象的創建的過程:
1、寫一個java類
2、寫一個spring的配置文件,把該類放入到spring容器中
< bean id=“helloWorld” class=“類的路徑”></ bean>
3、啓動spring容器
4、把對象從spring容器中取出來
5、對象調用方法
說明:bean中的id值是唯一的,不能出現相同的ID值

spring的scope

  • spring創建對象默認是單例模式

  • 如果把一個list聲明在該bean的屬性中,這樣list成了共享的數據,所以一般情況下把list聲明在方法中

  • 把spring創建的bean改成prototype模式,只需要寫一個屬性:scope爲prototype

spring什麼時候創建對象
1、在默認情況下,spring容器啓動的時候,就創建對象了
有利於開發:
當spring容器啓動的時候,如果配置文件中有錯誤,則會報錯
2、當從spring容器中獲取bean,當bean調用方法的時候創建對象
如果所有的bean按照這種模式加載,當spring容器啓動的時候是不會報錯的
如果一個bean中有大量的數據,需要的是用到的情況下加載

spring容器的初始化和銷燬:
前提條件:spring中的bean是單例的
1、在一個bean中可以提供init方法和destroy方法
2、當創建對象完成以後,直接執行init方法,該方法由spring容器來執行
3、當spring容器關閉的時候,執行destroy方法,該方法由spring容器來執行,由該方法釋放資源

說明:
如果spring中的bean是多例的情況下,spring容器不負責對象的銷燬,由程序員把該對象設置爲null
如果spring中的bean是多例,這個時候,不管在配置文件中lazy-init設置成什麼樣的值
在context.getBean時纔要創建對象,而且不負責銷燬

applicationContext.XML ----(Spring容器)

<?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">

    <!--將javabean放入spring容器中管理
       spring創建對象默認是單例設計模式-->
       
       <!--將javabean放入spring容器中 構造器實例化-->
    <bean id="test" class="IOC.dao.TestDaoImpl" >
        <property name="string" value="555"/>
    </bean>

    <!--別名-->
    <alias name="test" alias="good"/>


    <!--靜態工廠實例化 : 從指定工廠類中的調用靜態方法創建對象 
      引入工廠類,類中定義靜態方法getInstance,該方法返回new TestDaoImpl;那這個bean就是TestDaoImpl對象
-->
    <bean id="testFactory" class="IOC.dao.TestFactory" factory-method="getInstance"/>

    <!--實例工廠實例化 : 引入工廠bean  調用非靜態方法創建對象 -->
    <bean id="void" factory-bean="testFactory" factory-method="getInstance1"/>
*******************************************************************
客戶端的調用
        啓動spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("IOC/test/applicationContext.xml");

        把對象從spring容器取出來
          TestDaoImpl t2 = (TestDaoImpl) context.getBean("test");
          t2.sayHello();

spring的DI:依賴注入

原理:
1、啓動spring容器
2、spring容器爲person創建對象
3、利用property中的name屬性可以把set方法拼接出來
4、利用java的反射機制,給屬性賦值
說明:在默認的情況下,第3、4步在spring容器啓動的時候完成了,把這樣的過程叫做bean的裝配

public class Person{
    private Long id;
    private String name;
    private Student student;
    private Set sets;
    private List lists;
    private Map map;
    private Properties properties;  
}
      <bean id="student" class="IOC.dao.Student"/>

    <bean id="person" class="IOC.dao.Person" scope="singleton">

        <!--基本屬性賦值-->
        <property name="name" value="網二"/>
        <property name="id" value="222"/>

        <!--引用屬性-->
        <property name="student" ref="student"/>

        <!--集合屬性賦值-->
        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </set>
        </property>

        <property name="list">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </set>
        </property>

        <property name="map">
            <map>
                <entry key="key1" value="ma111"/>
                <entry key="key2" value-ref="student"/>
                <entry key="key3" value="ma111"/>
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="p111">倒計時卡鹿鼎記上課啦</prop>
                <prop key="p22">倒而我卻記上課啦</prop>
            </props>
        </property>
    </bean>

**************************************
   <!--構造器賦值-->
    <bean id="person1" class="IOC.dao.Person" scope="prototype">

        <constructor-arg index="0" value="mazi"/>
        <constructor-arg index="1" ref="student1"/>
    </bean>
    <bean id="student1" class="IOC.dao.Student"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章