spring学习第二天

使用spring容器的好处(IOC和DI的意义)

  • 客户端在使用时不用关注具体实现,完全面向接口编程,具体实现类有些配置文件的人来管

    applicationContext context = new classPathXmlApplicationContext("applicationContext");
    
    DocumentManager documentManager = new (DocumentManager)context.getBean("documentManager");
    

spring创建对象方法

  • 构造函数(用的最多)

  • 静态工厂

  • 实例工厂

  • 在默认情况下,spring产生的对象是单例的

  • prototype 原型模式(多例模式)

  • 如果scope = “prototype” 那么lazy-init将失去作用

spring容器创建对象的时间

方案1

1 加载spring容器
2 spring调用默认的构造函数为bean创建对象
3 利用context.getbean()将对象提取出来
4 好处:在启动spring容器的时候可以发现错误

方案2

1 启动spring容器
2 执行context.getbean
3 spring为bean创建对象
4 坏处:在启动spring容器的时候不能发现错误

spring容器init destroy

default

1 启动spring容器
2 创建对象
3 执行init方法 该方法是由spring容器自动调用的
4 context.getbean将对象提取出来
5 对象调用方法
6 当执行close方法的时候,执行该对象destroy方法 该方法是由spring容器自动调用的

scope=”prototype”

1 启动spring容器
2 context.getbean将对象提取出来
3 创建对象
4 执行init方法 该方法是由spring容器自动调用的
5 对象调用方法
6 不负责销毁

DI(依赖注入-xml方式)-给属性装配值

setter方法注入

  • property描述的就是bean中的属性

  • name就是属性名称

  • value就是属性值 如果基本类型(包含string)就用value赋值

  • ref 如果是引用类型就用ref赋值

  • 给属性赋值的过程称为装配

  • map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集。接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法

加断点来弄清加载顺序(构造函数, 方法)

  • list装配

    
        <property name="lists">
            <list>
                <value>l1</value>
                <ref bean = "person"/>
            </list>
        </property>
    
  • set装配

    
        <property name="sets">
            <set>
                <value>l1</value>
                <ref bean = "person"/>
            </set>
        </property>
    
  • map装配

    
        <property name="maps">
            <map>
                <entry key="01">
                    <value>m1</value>
                </entry>
                <entry key="02">
                    <ref bean = "person"/>
                </entry>
            </map>
        </property>
    
  • properties装配

    
        <property name="props">
            <props>
                <prop key="01">p1</prop>
                <prop key="02">p2</prop>
            </props>
        </property>
    
  • 依赖的优先级比scope高

构造器方法注入

  • constructor-arg 代表某个构造器参数

  • index 构造器参数的下标

  • value

  • ref

  • type 类型

  • <bean id= "person" class = "com.baisong.person">
        <constructor-arg index = "0" value = "aaaa" type = "java.lang.long"></constructor-arg>
        <constructor-arg index = "1" ref = "student"></constructor-arg>
    </bean>
  • setter与init的执行顺序

    public class DIXMLSetterInitDetroyTest {
       /**
        * 1、启动spring容器
        * 2、实例化person,student
        * 3、调用person的setter方法进行装配
        * 4、调用person的init方法
        * 5、context.getBean
        * 6、person调用方法完成任务
        */
       @Test
       public void testDI_XML_Setter_InitDestroy(){
           ApplicationContext context = 
                   new ClassPathXmlApplicationContext("applicationContext.xml");
           Person person = (Person)context.getBean("person");
           System.out.println(person.toString());
       }
       }

可以同时使用set方法和构造函数方法注入

《spring学习第二天》

    原文作者:SportCloud
    原文地址: https://segmentfault.com/a/1190000007614958
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞