栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 创建单例模式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 创建单例模式

springboot 单例模式的两种写法
  • 集成 service
    因为是单例模式,所有需要spring上下文注入进来,才能获取到
@Component
public class SpringContextUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static  T getBean(Class clazz) {
        return applicationContext.getBean(clazz);
    }

    public static  T getBean(String name, Class clazz) {
        return applicationContext.getBean(name, clazz);
    }

}

  • 单列模式:饿汉式(一般最好使用饿汉式来操作)
public class JdysEquipmentSinleton {

    private static  volatile JdysEquipmentSinleton INSYANCE;

    private int CODE;

    private IJdysEquipmentBillService jdysEquipmentBillServiceForNew;

    private JdysEquipmentSinleton(){
        jdysEquipmentBillServiceForNew = SpringContextUtils.getBean(IJdysEquipmentBillService.class);
    }

    public static JdysEquipmentSinleton getInstance(){
        if(null == INSYANCE){
            synchronized (JdysEquipmentSinleton.class){
                if(null == INSYANCE){
                    INSYANCE = new JdysEquipmentSinleton();
                }
            }
        }
        return INSYANCE;
    }
    public String getMaxEquipmentBillCode2(){
        if(0 != CODE){
            CODE += 1;
            return "D" + handelStrCode(CODE);

        }else {
            String maxEquipmentBillCode = DataUtil.isNotEmpty(jdysEquipmentBillServiceForNew.getMaxEquipmentBillCode()) ? jdysEquipmentBillServiceForNew.getMaxEquipmentBillCode() : "D00000";
            CODE += Integer.valueOf(maxEquipmentBillCode.substring(1,6));
            CODE += 1;
            return "D" + handelStrCode(CODE);
        }
    }
    public String handelStrCode(int code){
        int length = String.valueOf(code).length();
        int num = 0;
        if(length < 5){
            num = 5 - length;
        }
        String numCode = "";
        for (int i = 0; i < num; i++) {
            numCode += "0";
        }
        return numCode + code;
    }

  • 一般常用的懒汉式单例
 private LazyMan(){
       
        System.out.println(Thread.currentThread().getName() + "ok");
    }


    
    private volatile static LazyMan lazyMan;

    // 防止多线程出现问题,就使用了双重检测锁模式,懒汉式单列,     简称 DCL 懒汉式
    public static LazyMan getInstance(){
        if(null == lazyMan){
            synchronized (LazyMan.class){
                if(null == lazyMan){
                    lazyMan = new LazyMan();  //在极端情况下不是一个原子性的操作
                    
                }
            }
        }
        return lazyMan;  //当B线程进来的时候可以能 lazyMan 还没重拍好
    }
  • 使用单列模式和反射斗智
package com.central.jdys.config;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

// 懒汉式单例(单列模式只会加载一次)
public class LazyMan {

    private static boolean LazyFlag = false;

    private LazyMan(){
        // 保护反射机制来破坏
        synchronized (LazyMan.class){
            if(LazyFlag == false){
                LazyFlag = true;
            }else {
                throw new RuntimeException("不要用反射来破坏");
            }
        }

        System.out.println(Thread.currentThread().getName() + "ok");
    }


    
    private volatile static LazyMan lazyMan;

    // 防止多线程出现问题,就使用了双重检测锁模式,懒汉式单列,     简称 DCL 懒汉式
    public static LazyMan getInstance(){
        if(null == lazyMan){
            synchronized (LazyMan.class){
                if(null == lazyMan){
                    lazyMan = new LazyMan();  //在极端情况下不是一个原子性的操作
                    
                }
            }
        }
        return lazyMan;  //当B线程进来的时候可以能 lazyMan 还没重拍好
    }


    public static void main(String[] args) throws Exception {

        
//
//        LazyMan instance = LazyMan.getInstance();  //通过单列加载
//        // 反射可以破坏单例
//        Constructor declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
//        declaredConstructor.setAccessible(true);  //无视 私有关键字
//        LazyMan lazyMan = declaredConstructor.newInstance();  //直接new一个对象

        

        
        Field lazyFlag = LazyMan.class.getDeclaredField("LazyFlag");
        lazyFlag.setAccessible(true);


        // 反射可以破坏单例
        Constructor declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true);  //无视 私有关键字
        LazyMan instance = declaredConstructor.newInstance();  //直接new一个对象

        
        lazyFlag.set(instance,false);

        LazyMan lazyMan = declaredConstructor.newInstance();  //直接new一个对象

        System.out.println(instance);
        System.out.println(lazyMan);
    }

    

    public static class InnerClass{

    }
}

  • 使用枚举
    – 枚举是没用办法使用反射来破解的,枚举没有无参构造,只有一个有参构造,一个是 String,一个是int类型,强行使用反射,jdk会直接抛出异常
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643999.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号