zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    《构建之法》阅读笔记二
    《构建之法》阅读笔记一
    软件工程个人课程总结
    纯随机数生成器
    递归方法
    素数的输出
    字母统计|英语的26 个字母在一本小说中是如何分布的
    类的声明
    FileInputStream类与FileOutputStream类
    验证码|程序登录界面
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4573913.html
Copyright © 2011-2022 走看看