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();
}
}
Spring 对象产生过程
最新推荐文章于 2023-12-21 09:18:03 发布