加载属性并注入最终变量-Java

加载属性并注入最终变量-Java,java,spring,spring-boot,properties,switch-statement,Java,Spring,Spring Boot,Properties,Switch Statement,我想将属性值加载到一个constants类中。我可以通过在类构造函数或静态初始化中注入值来实现,如下所示 public class MyConstants { public final String CONSTANT; Myconstants() { //read property and set to this.CONSTANT } } 以上初始化工作正常。但当我必须在switch case语句中使用常量时,问题就出现了。它会给我错误,因为那个点的常

我想将属性值加载到一个constants类中。我可以通过在类构造函数或静态初始化中注入值来实现,如下所示

public class MyConstants {
    public final String CONSTANT;

    Myconstants() {
        //read property and set to this.CONSTANT
    }
}
以上初始化工作正常。但当我必须在switch case语句中使用常量时,问题就出现了。它会给我错误,因为那个点的常数不是常数,而是空白的最终字段

是否有任何方法可以读取属性文件并将值设置为常量,以便不存在编译错误?[类似于在编译时加载属性和设置本身]。我正在使用spring,这个用例是否有spring或java解决方案。我的意图是不为每一次不断的变化而构建。另外,您将如何处理这个用例


谢谢

switch语句中的字符串是“硬编码的”。实际上,在java中生成一个switch/case语句是不可能的,因为它的“case”元素在编译时没有被锁定。如果您想在应用程序初始化期间从属性文件中加载这些字符串“常量”,则它们不是常量

或者至少,就java基于字符串的开关/案例的需求而言,这不是常数

相反,可以使用某种类型的
java.util.Map
(将字符串值映射到runnables或其他表示您现在放入case块中的内容的内容),或者使用一组if/else if语句

编辑:如何使用地图来执行此操作。
@functioninterface
公共接口命令处理器{
字符串应用(字符串cmd);
}
公共类命令处理程序{
专用映射处理器=Map.of(
“你好”,cmd->“你好,”+cmd,
“时间”,cmd->“它是”+System.currentTimeMillis()+“过去的1970年。”,
“大写”,字符串::toUpperCase);
公共静态void main(字符串[]args)引发异常{
扫描仪s=新的扫描仪(System.in);
s、 使用分隔符(“\r?\n”);
命令处理器未知命令=
cmd->“已知命令:”+processors.keySet();
while(true){
字符串行=s.next();
String[]parts=line.split(“\\s+”,2);
var processor=processors.getOrDefault(parts[0],未知命令);
字符串回答=处理器.apply(parts.length>1?parts[1]:“”);
System.out.println(应答);
}
}
}

通常,属性文件由可在运行时加载的“key=value”对组成。环境变量也可以类似地使用。通常,常量是关键,值用于影响应用程序逻辑。只要键保持不变,就可以根据需要更改属性值,而无需重新编译,并且可以在switch语句中使用可能值的范围。例:

    public static void main(String[] args)
    {
        AppConfigParam<Type1> type1   = new AppConfigParam<>("Param1", Type1::valueOf);
        AppConfigParam<Integer> type2 = new AppConfigParam<>("Param2", Integer::parseInt);
        switch(type1.value().orElseThrow(() -> new IllegalStateException("Missing type"))) { case A: break; }
        switch(type2.value().orElseThrow(() -> new IllegalStateException("Missing type"))) { case 1: break; }
    }

    public enum Type1{ A, B, C }

    private static final class AppConfigParam<T>
    {
        private final String constant;
        private final Function<? super String, T> resolver;
        AppConfigParam(String constant, Function<? super String, T> resolver){ this.constant = constant; this.resolver = resolver; }
        public Optional<T> value(){ return Optional.ofNullable(PARAMS.get(constant)).map(resolver); }
        private static final Map<String, String> PARAMS = System.getenv();
    }
publicstaticvoidmain(字符串[]args)
{
AppConfigParam type1=新的AppConfigParam(“Param1”,type1::valueOf);
AppConfigParam type2=新的AppConfigParam(“Param2”,整数::parseInt);
开关(type1.value().orelsetrow(()->new IllegalStateException(“缺少类型”)){case A:break;}
开关(type2.value().orelsetrow(()->new IllegalStateException(“缺少类型”)){case 1:break;}
}
公共枚举类型1{A,B,C}
私有静态最终类AppConfigParam
{
私有最终字符串常量;

private final Function将有助于您提供导致某些错误的版本。@Mikheilzhghhenti我认为错误是java错误(所有版本都有)这说明常量应该在case语句中。我理解错误。但我想知道用例的其他方法。我可以做if-else流,但会有很多if-else。我不理解你提到的可运行方法。你能解释一下吗?@sachin我已经编辑了答案以包含一个示例。在你的示例中,地图键仍然是常量。很难想象一个键不是常量的用例…
    public static void main(String[] args)
    {
        AppConfigParam<Type1> type1   = new AppConfigParam<>("Param1", Type1::valueOf);
        AppConfigParam<Integer> type2 = new AppConfigParam<>("Param2", Integer::parseInt);
        switch(type1.value().orElseThrow(() -> new IllegalStateException("Missing type"))) { case A: break; }
        switch(type2.value().orElseThrow(() -> new IllegalStateException("Missing type"))) { case 1: break; }
    }

    public enum Type1{ A, B, C }

    private static final class AppConfigParam<T>
    {
        private final String constant;
        private final Function<? super String, T> resolver;
        AppConfigParam(String constant, Function<? super String, T> resolver){ this.constant = constant; this.resolver = resolver; }
        public Optional<T> value(){ return Optional.ofNullable(PARAMS.get(constant)).map(resolver); }
        private static final Map<String, String> PARAMS = System.getenv();
    }