Spring初识

一.Ioc(控制反转)

1.Spring创建对象的三种方式

1)默认构造函数
"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())时候会执行对象的构造方法
"helloWorld2" 
    class="com.spring.createobject.when.HelloWorld"
bean>

如果指定lazy-init=”true”,则在getBean()时执行对象的构造方法

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

特别声明:每个bean对应一个实例对象,会执行每个实例对象的构造方法

3.对象的多实例和单实例

默认情况下,创建的对象是单实例的,通过多次getBean()获取对象的hashcode是一样的

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

指定scope=”prototype” ,通过getBean()获取的对象都是多实例的

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

二.DI(依赖注入)

1.通过set方法注入

"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>list1value>
                <value>list2value>
                <value>list3value>
                <ref bean="student"/>
            list>
        property>

        <property name="set">
            <set>
                <value>set1value>
                <value>set2value>
                <value>set3value>
                <ref bean="student"/>
            set>
        property>

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

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

2.通过构造器注入

"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属性

"person" 
    class="com.spring.di.xml.extend.Person">
        <property name="name" value="大明">property>
bean>

<bean id="student"
    class="com.spring.di.xml.extend.Student" parent="person">
 bean>    

1.通过setXXX方法注入

"person" 
    class="com.spring.di.xml.extend.Person">
        <property name="name" value="大明">property>
bean>

<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指定的方法

你可能感兴趣的:(JavaWeb)