【问题标题】:POJO from properties file in Spring来自 Spring 中的属性文件的 POJO
【发布时间】:2020-12-16 10:50:32
【问题描述】:

假设我有一个像这样的 POJO 类:

class Foo {
    String name;
    int id;
}

我如何在属性文件中指定这个对象,以便我可以做这样的事情:

Foo foo = environment.getProperty("foo", Foo.class);

这个我试过了,还是不行:

foo.name = "My Name"
foo.id = 1234

我不想使用@ConfigurationProperties,因为我想在运行时读取属性。

【问题讨论】:

    标签: java spring spring-boot spring-data


    【解决方案1】:

    使用它 V1 是 springboot1.x (copy from shardingsphere)

     private static Object v1(Environment environment, String prefix, boolean handlePlaceholder) {
        try {
            Class<?> resolverClass = Class.forName("org.springframework.boot.bind.RelaxedPropertyResolver");
            Constructor<?> resolverConstructor = resolverClass.getDeclaredConstructor(PropertyResolver.class);
            Method getSubPropertiesMethod = resolverClass.getDeclaredMethod("getSubProperties", String.class);
            Object resolverObject = resolverConstructor.newInstance(environment);
            String prefixParam = prefix.endsWith(".") ? prefix : prefix + ".";
            Method getPropertyMethod = resolverClass.getDeclaredMethod("getProperty", String.class);
            Map<String, Object> dataSourceProps = (Map)getSubPropertiesMethod.invoke(resolverObject, prefixParam);
            Map<String, Object> propertiesWithPlaceholderResolved = new HashMap();
            Iterator var11 = dataSourceProps.entrySet().iterator();
    
            while(true) {
                while(var11.hasNext()) {
                    Entry<String, Object> entry = (Entry)var11.next();
                    String key = (String)entry.getKey();
                    Object value = entry.getValue();
                    if (handlePlaceholder && value instanceof String && ((String)value).contains("${")) {
                        String resolvedValue = (String)getPropertyMethod.invoke(resolverObject, prefixParam + key);
                        propertiesWithPlaceholderResolved.put(key, resolvedValue);
                    } else {
                        propertiesWithPlaceholderResolved.put(key, value);
                    }
                }
    
                return Collections.unmodifiableMap(propertiesWithPlaceholderResolved);
            }
        } catch (Throwable var16) {
            throw var16;
        }
    }
    
    private static Object v2(Environment environment, String prefix, Class<?> targetClass) {
        try {
            Class<?> binderClass = Class.forName("org.springframework.boot.context.properties.bind.Binder");
            Method getMethod = binderClass.getDeclaredMethod("get", Environment.class);
            Method bindMethod = binderClass.getDeclaredMethod("bind", String.class, Class.class);
            Object binderObject = getMethod.invoke((Object)null, environment);
            String prefixParam = prefix.endsWith(".") ? prefix.substring(0, prefix.length() - 1) : prefix;
            Object bindResultObject = bindMethod.invoke(binderObject, prefixParam, targetClass);
            Method resultGetMethod = bindResultObject.getClass().getDeclaredMethod("get");
            return resultGetMethod.invoke(bindResultObject);
        } catch (Throwable var10) {
            throw var10;
        }
    }
    

    【讨论】:

    • 非常感谢,这行得通。但是,我注意到当前缀有下划线等时它会失败。
    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 2016-11-10
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2017-07-26
    相关资源
    最近更新 更多