Spring容器中 singleton 单例、 prototype多例

项目开发中通常会使用:singleton 单例、 prototype多例

Singleton: 在一个spring容器中,对象只有一个实例。(默认值

Prototype: 在一个spring容器中,存在多个实例,每次getBean 返回一个新的实例。

 

定义spring容器,applicationContext.xml:

    <!--

       bean的作用范围

       scope:配置作用范围的,默认值就是singleton单例

     -->

    <!-- 单例 -->

    <!-- 
    <bean id="singletonBean" class="com.igeek.scope.SingletonBean"/>
    -->

    <bean id="singletonBean" class="com.igeek.scope.SingletonBean" scope="singleton"/> 

    <!-- 多例 -->

    <bean id="prototypeBean" class="com.igeek.scope.PrototypeBean" scope="prototype"/>

猜你喜欢

转载自blog.csdn.net/qq_15204179/article/details/82862192