Spring 对象产生过程

public class testHelloWorldWhen {

    /**
     *  默认情况下,在srping启动的时候就将配置文件中的 声明对象进行创建
     *  1.启动spring容器
     *  2.spring容器调用默认的构造函数为bean 创建对象
     *  3.利用context.getBean()将对象提取出来
     *
     *
     *  这一种形式更安全,如果spring 的配置文件有错误,
     *  那么在启动spring 容器的时候将会报错
     * */

    @Test
    public void testCreateObject_When_Default_Lazy_Init_False(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld=(HelloWorld)context.getBean("helloWorld");
        helloWorld.sayHello();
    }

    /**
     * 1.启动spring容器
     * 2.context.getBean()
     * 3.spring 容器为该bean创建对象
     * 在启动spring 容器的时候,可能发现不了配置文件的错误
     * */

    @Test
    public void testCreateObject_When_Default_Lazy_Init_True(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //在此时,对象还没有被spring 容器创建
        HelloWorld helloWorld=(HelloWorld)context.getBean("helloWorld");
        helloWorld.sayHello();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值