服务器中的单例模式:启动时就进行初始化

服务器中的单例模式:启动时就进行初始化,第1张

服务器中的单例模式:启动时就进行初始化

pom.xml

        
            org.reflections
            reflections
            0.9.10
        

IInit.java

package org.example.testsingleton;

public interface IInit {
    void init();
}

Main.java

package org.example.testsingleton;

import org.reflections.Reflections;

import java.util.Set;

public class Main implements IInit {
    public static int MIN_NAME_LEN = 0;

    @Override
    public void init() {
        MIN_NAME_LEN = 1;
    }

    public static void login(String userName) {
        if (userName.length() < MIN_NAME_LEN) {
            System.out.println("名字过短");
        } else {
            System.out.println("ok");
        }
    }

    public static void main(String[] args) {
        Reflections reflections = new Reflections("org.example.testsingleton");
        Set> set = reflections.getSubTypesOf(IInit.class);
        set.forEach(c -> {
            try {
                IInit inst = c.getConstructor().newInstance();
                inst.init();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        System.out.println(Main.MIN_NAME_LEN);
        Main.login("11");
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5696801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存