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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值