Spring Bean的作用域

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 5 
 6     <!--默认构造方法实例化-->
 7     <bean id="personService" class="org.zln.module.test2.service.impl.PersonServiceBean" scope="prototype"/><!-- property作用域的Bean每次调用都会生成新的实例 -->
 8     <!--静态工厂方法实例化-->
 9     <bean id="personService2" class="org.zln.module.test2.service.impl.PersonServiceBean" factory-method="getPersonServiceBean" scope="singleton"/><!-- singleton作用域的Bean是单实例的,也是默认配置 -->
10     <!--实例工厂方法-->
11     <bean id="personServiceFactory" class="org.zln.module.test2.service.impl.PersonServiceBean"/>
12     <bean id="personService3" factory-bean="personServiceFactory" factory-method="getPersonServiceFactory"/>
13 
14 </beans>
15 
16 <!-- 
17 除了singleton与prototype外,对应于Web环境,还有三种作用域
18 request
19 session
20 globalsession
21 
22 此外,每个Bean还可以设置lazy-init属性,这样就只有当调用的时候才初始化,但不推荐这么干。最好是在容器启动的时候就对类进行初始化,提早暴露可能存在的问题
23 如果需要对配置文件中的所有Bean都实现延迟加载,    default-lazy-init="true"
24  -->
25 <?xml version="1.0" encoding="UTF-8"?>
26 <beans xmlns="http://www.springframework.org/schema/beans"
27        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
29     default-lazy-init="true">
30 
31 
32 </beans>

 

posted @ 2015-06-13 18:45  csnmd  阅读(164)  评论(0编辑  收藏  举报