Spring初識

一.Ioc(控制反轉)

1.Spring創建對象的三種方式

1)默認構造函數
<bean id="helloWorld" 
    class="com.spring.createobject.method.HelloWorld">
</bean> 

(2)靜態工廠方式
<bean id="helloWorld2" 
    class="com.spring.method.factory.HelloWorldStaticFactory" 
    factory-method="getInstance">
</bean>
(3)實例工廠模式
<bean id="helloWorldFactory" 
    class="com.spring.method.factory.HelloWorldFactory">
</bean>
<bean id="helloWorld3" 
    factory-bean="helloWorldFactory"
    factory-method="getInstance">
</bean>

2.創建對象的時間

默認情況下,是在啓動Spring容器的時候創建對象(new ClassPathXmlApplicationContext())時候會執行對象的構造方法
<bean id="helloWorld2" 
    class="com.spring.createobject.when.HelloWorld"
bean>

如果指定lazy-init=”true”,則在getBean()時執行對象的構造方法

<bean id="helloWorld2" 
    class="com.spring.createobject.when.HelloWorld"
lazy-init="true"></bean>

特別聲明:每個bean對應一個實例對象,會執行每個實例對象的構造方法

3.對象的多實例和單實例

默認情況下,創建的對象是單實例的,通過多次getBean()獲取對象的hashcode是一樣的

<bean id="helloWorld" 
    class="com.spring.createobject.when.HelloWorld"></bean>

指定scope=”prototype” ,通過getBean()獲取的對象都是多實例的

<bean id="helloWorld2" 
    class="com.spring.createobject.when.HelloWorld" 
    scope="prototype">
</bean>

二.DI(依賴注入)

1.通過set方法注入

<bean id="person" class="com.spring.di.xml.setter.Person">
        <property name="pid" value="20"></property>
        <property name="name" value="大明"></property>
        <property name="student" ref="student"></property>
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
                <ref bean="student"/>
            </list>
        </property>

        <property name="set">
            <set>
                <value>set1</value>
                <value>set2</value>
                <value>set3</value>
                <ref bean="student"/>
            </set>
        </property>

        <property name="map">
            <map>
                <entry key="m1">
                    <value>map1</value>
                </entry>
                <entry key="m2">
                    <value>map1</value>
                </entry>
                <entry key="m3">
                    <ref bean="student"/>
                </entry>
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="p1">p1111</prop>
                <prop key="p2">p2222</prop>
            </props>
        </property>
        <property name="objects">
            <list>
                <value>o1</value>
                <ref bean="student"/>
            </list>
        </property>
    </bean>
    <bean id="student" class="com.spring.di.xml.setter.Student"></bean>

2.通過構造器注入

<bean id="student" 
    class="com.spring.di.xml.constructor.Student">
</bean>

<bean id="person" 
    class="com.spring.di.xml.constructor.Person">
        <constructor-arg index="0" value="大明1566">
        </constructor-arg>
        <constructor-arg index="1" ref="student">
        </constructor-arg>
        <property name="pid" value="30000">
        </property>
</bean>

對應構造函數:
public Person(String name,Student student){     
        this.name = name;
        this.student = student;
}

三.Spring的繼承

1.通過parent屬性

<bean id="person" 
    class="com.spring.di.xml.extend.Person">
        <property name="name" value="大明"></property>
</bean>
<!-- 在父類中賦值,子類通過parent屬性繼承父類的內容 -->
<bean id="student"
    class="com.spring.di.xml.extend.Student" parent="person">
 </bean>    

1.通過setXXX方法注入

<bean id="person" 
    class="com.spring.di.xml.extend.Person">
        <property name="name" value="大明"></property>
</bean>
<!-- 因爲java的繼承機制,子類繼承了父類的setXXX方法,所以可以通過setXXX方法注入 -->
<bean id="student2" 
    class="com.spring.di.xml.extend.Student">
        <property name="name" value="大明1566"></property>
</bean>

四.Spring啓動流程

1.Spring創建容器中的對象(調用容器的構造函數)
2.Spring給容器屬性裝配值(setXXX)
3.調用init-method指定的方法
4.context.getBean獲取對象,調用業務邏輯方法
5.在Spring容器銷燬的時候調用destory-method指定的方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章