java – 由Typesafe配置支持的Spring环境

我想在我的项目中使用typesafe配置(HOCON配置文件),这有助于简化和组织应用程序配置.目前我正在使用普通的Java属性文件(application.properties),这在大项目中很难处理.

我的项目是Spring MVC(不是Spring启动项目).有没有办法支持我的Spring环境(我将注入到我的服务中)以支持typesafe配置.这不应该像@Value注释,@ Autowired Environment等那样制止我现有的环境使用.

如何以最小的努力和我的代码更改来完成此操作.

这是我目前的解决方案:寻找还有其他更好的方法

@Configuration
public class PropertyLoader{
    private static Logger logger = LoggerFactory.getLogger(PropertyLoader.class);

    @Bean
    @Autowired
    public static PropertySourcesPlaceholderConfigurer properties(Environment env) {
        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();

        Config conf = ConfigFactory.load();
        conf.resolve();
        TypesafePropertySource propertySource = new TypesafePropertySource("hoconSource", conf);

        ConfigurableEnvironment environment = (StandardEnvironment)env;
        MutablePropertySources propertySources = environment.getPropertySources();
        propertySources.addLast(propertySource);
        pspc.setPropertySources(propertySources);

        return pspc;
    }
}

class TypesafePropertySource extends PropertySource<Config>{
    public TypesafePropertySource(String name, Config source) {
        super(name, source);
    }

    @Override
    public Object getProperty(String name) {
        return this.getSource().getAnyRef(name);
    }
}
最佳答案
我认为我提出了比手动将PropertySource添加到属性源更加惯用的方法.创建一个PropertySourceFactory并使用@PropertySource引用它

首先,我们有一个类似于你拥有的TypesafeConfigPropertySource:

public class TypesafeConfigPropertySource extends PropertySource<Config> {
    public TypesafeConfigPropertySource(String name, Config source) {
        super(name, source);
    }

    @Override
    public Object getProperty(String path) {
        if (source.hasPath(path)) {
            return source.getAnyRef(path);
        }
        return null;
    }
}

接下来,我们创建一个返回该属性源的PropertySource工厂

public class TypesafePropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        Config config = ConfigFactory.load(resource.getResource().getFilename()).resolve();

        String safeName = name == null ? "typeSafe" : name;
        return new TypesafeConfigPropertySource(safeName, config);
    }

}

最后,在我们的配置文件中,我们可以像任何其他PropertySource一样引用属性源,而不必自己添加PropertySource:

@Configuration
@PropertySource(factory=TypesafePropertySourceFactory.class, value="someconfig.conf")
public class PropertyLoader {
    // Nothing needed here
}

转载注明原文:java – 由Typesafe配置支持的Spring环境 - 代码日志