java - Spring Constant 将忽略并非所有大写字段

标签 java spring

如果有一些public static final字段, 那么 spring Contants 类可以帮助为这些字段提供帮助。

但是对于小写字段名称, Contants 处理不好。 Java 规范建议 我们使用所有大写作为public static final字段名称, 但小写名称仍然有效。

参见示例:

public class Test {
    public static final int a = 1;
    public static final int B = 2;

    public static void main(String[] args) {
        org.springframework.core.Constants constants = new org.springframework.core.Constants(Test.class);
        System.out.println(constants.asNumber("b").intValue()); // line 1
        System.out.println(constants.asNumber("a").intValue()); // line 2
    }
}

第1行将输出2, 但第 2 行抛出 ConstantException:在类 Test 中找不到字段“A”

Why does not they compare them case sensitive?

最佳答案

由于约定,Java 中的编译时常量始终UPPER_UNDERSCORE。无一异常(exception)。

来自documentation for Constants

Consider class Foo containing public final static int CONSTANT1 = 66; An instance of this class wrapping Foo.class will return the constant value of 66 from its asNumber method given the argument "CONSTANT1".

因此,虽然根据 Java 编程语言,采用 camelCase 或(但愿不会)lower_underscore 的名称是有效;这是否违反了公认的惯例并且不值得鼓励。

关于java - Spring Constant 将忽略并非所有大写字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36234360/

相关文章:

java - 如何处理 Nimbus Look and Feel 中的衍生颜色?

java - Google 登录 Firebase 验证,显示 "choose account"

java - CompareTo() 和 Collections.sort() 多列排序(升序)问题的解决方案

java - 在java中将字符数组转换为对象数组?是否可以?

spring - SQL 错误 : 23503, SQLState : 23503 in SpringBoot 2. 1.4.RELEASE 应用程序

java - 使用嵌入式 Jetty 进行身份验证

java - 如何从Gson链接获取数据

java - Jpa不刷新删除

java - 如果我的库也可能有带有我不知道的 Order 注释的 ControllerAdvices,我应该在 ControllerAdvice 上使用什么 order 值?

java - 出现错误 org.hibernate.QueryException : illegal attempt to dereference collection