繁体   English   中英

Spring 中的属性文件中的 POJO

[英]POJO from properties file in Spring

假设我有一个 POJO class 像这样:

class Foo {
    String name;
    int id;
}

如何在属性文件中指定此 object 以便我可以执行以下操作:

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

我试过这个,但它不起作用:

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

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

使用它 V1 是 springboot1.x(从 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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM