Spring中的单例和多例

Author Avatar
cuteximi 9月 19, 2017
  • 在其它设备中阅读本文章

直接上配置:

<?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可以避免预处理

This blog is under a CC BY-NC-SA 3.0 Unported License
本文链接:http://blog.cuteximi.com/Spring中的单例和多例/