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>
  • 相关阅读:
    海康 大华 华为 宇视等安防摄像头、NVR、平台网关通过GB28181接入LiveGBS流媒体服务实现WEB无插件直播
    工厂模式
    装饰者模式
    观察者模式
    Android 滑动事件冲突解决 Touch事件处理机制
    java 策略模式
    java判断手机访问还是电脑访问
    swift中闭包和OC中block的用法比较
    Swift小技巧(五)
    Swift小技巧(三)
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4573913.html
Copyright © 2011-2022 走看看