Spring容器创建对象的时间

Spring容器创建bean有两种方式:一是加载容器时即创建对象,通过默认构造函数;二是设置Lazy-init为true,执行getBean时才创建。推荐启动时创建,能尽早发现配置错误。

spring容器创建对象的时间又两种情况:

情况一:当程序加载Spring 容器时就会调用默认的构造函数为bean创建对象

1. 首先,我们创建一个类

public class HelloWorldScope {
	public HelloWorldScope() {
		System.out.println("This is the Constructor!");
	}
	public void hello() {
		System.out.println("Hello World!");
	}
}
2. 配置spring容器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- 
		beans 存放了很多个类
		把一个类放入到spring容器中,该类就是bean
	 -->
	 <!-- 
	 	一个bean就是一个描述一个类
	 	id就是标示符
	 			命名规范:类名的第一个大写字母变成小写,其他字母不变
	 	class为类的全名
	  -->
	<bean id="helloWorldScope" 
		class="com.yangguang.spring.helloworld2.HelloWorldScope"
		scope="prototype">
	</bean>
</beans>

3. 编写测试代码

/**
	 * 1.加载spring容器
	 * 2.spring容器调用默认的构造函数为bean创建对象
	 * 3.利用context.getBean把对象取出来
	 * 
	 * 说明:该形式更加安全,如果spring的配置文件中有错误,
	 * 		在启动spring容器的时候就会报错
	 */
	@Test
	public void testScope_Lazy_init_false() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorldScope helloWorldScope = (HelloWorldScope) context.getBean("helloWorldScope");
		HelloWorldScope helloWorldScope2 = (HelloWorldScope) context.getBean("helloWorldScope");
	}


通过设置断点调试程序可知,当程序执行第一条语句加载spring容器:ApplicationContext context = ......时,spring容器就会执行类中的构造函数为bean创建一个对象



     情况二:当执行context.getBean时,spring容器为该bean创建对象

1. 与情况一类相同

2. 配置spring容器(与情况一种相比,增加了Lazy-init="true")

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- 
		beans 存放了很多个类
		把一个类放入到spring容器中,该类就是bean
	 -->
	 <!-- 
	 	一个bean就是一个描述一个类
	 	id就是标示符
	 			命名规范:类名的第一个大写字母变成小写,其他字母不变
	 	class为类的全名
	  -->
	
	<bean id="helloWorldScope" 
		class="com.yangguang.spring.helloworld2.HelloWorldScope"
		scope="prototype"
		lazy-init="true">
	</bean>
</beans>

3. 编写测试代码:(与情况一相同)

/**
	 * 1.启动spring容器
	 * 2.执行context.getBean
	 * 3.spring容器为该bean创建对象
	 */
	@Test
	public void testScope_Lazy_init_true() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorldScope helloWorldScope = (HelloWorldScope) context.getBean("helloWorldScope");
		HelloWorldScope helloWorldScope2 = (HelloWorldScope) context.getBean("helloWorldScope");
		
	}
   情况二中经过设置断点调试,发现程序先启动加载pring容器,然后调用context.getBean(),这时spring容器才为该bean创建对象



总结:推荐使用情况一种的这种方法。该形式更加安全,如果spring的配置文件中有错误,在启动spring容器的时候就会报错


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值