SpringMVC:静态类托管

1、原始的实现单例模式

public class SingletonOne {
    private static SingletonOne instance = null;
    private SingletonOne() {}
    public static SingletonOne getInstance() {
        if (instance == null) {
            instance = new SingletonOne(); 
        }
        return instance;
    }
}

这种方法有一个弊端,存在线程不安全

比如当两个线程同时进入if(instance == null)时,一个线程判断了当前为空,然后切换到另一个线程,这个线程也判断为空。然后切换回第一个线程,进行实例化,再切换到第二个线程,进行实例化。这样就存在了两个实例。

2、通过关键字Synchronized强制线程同步 

package com.kingbal.tst.singleton;

public class SingletonTwo {
    private static SingletonTwo instance = null;
    private SingletonTwo() {}
    public static synchronized SingletonTwo getInstance() {
        if (instance == null) {
            instance = new SingletonTwo();
        }
        return instance;
    }
}

这样当线程进行到getInstance会同步的进行,不会有线程安全问题,但是不仅仅是实例化,每次调用也需要同步,这样就会造成很多资源的浪费。

3、通过静态内部类进行单例

public class SingletonThree {
    private static class SingletonHolder{
        static SingletonThree instance = new SingletonThree();
    }
    
  private SingletonThree() {}
        
    public static SingletonThree getInstance() {
            return SingletonHolder.instance;
    }
}

这种方法时最推荐的一种方法,由于Java的调用机制,SingletonHolder只有在调用getInstance的时候才会加载,而内部的静态类只会被加载一次,因此又是线程安全的。

总结起来:

第一种方法:是存在线程安全问题的。

第二种方法:则消耗了一定的资源。

第三种方法:比较推荐。

下面通过spring的factory-method来创建单例的bean

1.1、先创建一个单例对象
package com.kingbal.tst.factorymethod;

public class Stage {
    public void perform(){
        System.out.println("演出开始...");
    }
    private Stage(){
        
    }
    private static class StageSingletonHolder{
        static Stage instance = new Stage();
    }
    public static Stage getInstance(){
        return StageSingletonHolder.instance;
    }
}
1.2、在spring配置文件中指定加载的方法getInstance
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="theStage" class="com.kingbal.tst.factorymethod.Stage"
         factory-method="getInstance"></bean>
</beans>
1.3、通过应用上下文调用bean获取实例
package com.kingbal.tst.factorymethod;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Stage stage = ((Stage)ctx.getBean("theStage")).getInstance();
        stage.perform();
    }

}
执行结果:
Loading XML bean definitions from class path resource [bean.xml]
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2d1879ea: defining beans [duke,sonnet29,poeticDuke,theStage]; root of factory hierarchy
演出开始...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值