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">  
     
    <bean id="helloWorld" class="com.HelloWorld" scope="prototype" init-method="init" destroy-  
  
method="destroy"></bean>  
</beans>  

其中:
init-method,该方法是由spring容器去执行法人,在构造函数之后执行;
destroy-method,如果是单例模式,则spring容器关闭或者销毁的时候,会执行该方法;如果是多例模式,则spring容器不负责销毁;

说明:
①:如果想让spring容器管理bean的生命周期,那么该bean必须为单例。
②:当一个bean在多例模式下,lazy-init为false或者default失效.
③:单例模式下,lazy-init可以避免预处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值