Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring(非启动)从多个项目加载多个yml文件_Java_Spring_Spring Profiles_Spring Properties - Fatal编程技术网

Java Spring(非启动)从多个项目加载多个yml文件

Java Spring(非启动)从多个项目加载多个yml文件,java,spring,spring-profiles,spring-properties,Java,Spring,Spring Profiles,Spring Properties,因此,我阅读了dosens af关于如何配置Spring boot以了解比application.yml更多的yml文件以及如何包含这些文件的文章,甚至是从子项目中。然而,很难找到描述“纯”春天的文章。然而,我认为我正朝着正确的方向前进,我只是无法找回我的配置值 这是一个直接的多项目渐变构建,为了简单起见,它包含两个项目。一个项目是“主”spring项目,即spring上下文在此项目中初始化。另一个是带有一些数据库实体和数据源配置的“支持”模块。我们使用基于注释的配置 我希望能够在支持模块中定义

因此,我阅读了dosens af关于如何配置Spring boot以了解比
application.yml更多的yml文件以及如何包含这些文件的文章,甚至是从子项目中。然而,很难找到描述“纯”春天的文章。然而,我认为我正朝着正确的方向前进,我只是无法找回我的配置值

这是一个直接的多项目渐变构建,为了简单起见,它包含两个项目。一个项目是“主”spring项目,即spring上下文在此项目中初始化。另一个是带有一些数据库实体和数据源配置的“支持”模块。我们使用基于注释的配置

我希望能够在支持模块中定义一组配置属性,并基于任何活动的spring概要文件,相应地加载数据源配置

SA post让我对不同答案中的不同链接进行了深入了解,并以此为基础构建了我的解决方案。结构和代码如下:

mainproject
  src
    main
      groovy
        Application.groovy
    resourcers
      application.yml

submodule
  src
    main
      groovy
        PropertiesConfiguration.groovy
        DataSource.groovy
    resources
      datasource.yml
属性配置.groovy
通过使用
属性资源占位符配置器添加
数据源.yml

@Configuration
class PropertiesConfiguration {

    @Bean
    public PropertySourcesPlaceholderConfigurer configure() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer()
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean()
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("datasource.yml"))
        configurer.setProperties(yamlPropertiesFactoryBean.getObject())
        return configurer
    }
}
然后,
Datasource.groovy
应使用(为可读性而减少的代码)读取基于spring配置文件的值:

env.getProperty
返回null。无论哪个弹簧配置文件处于活动状态。我可以使用@value注释访问配置值,但是活动配置文件不受尊重,即使没有为该配置文件定义,它也会返回一个值。我的yml看起来像这样:

---
spring:
  profiles: development

datasource:
  username: sa
  password:
  databaseUrl: jdbc:h2:mem:tests
  databaseDriver: org.h2.Driver
@Autowired
public void config (final ConfigurableEnvironment confenv) {
    final YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
    try {
        final PropertySource<?> datasource =
                yamlPropertySourceLoader.load("datasource",
                                            new ClassPathResource("datasource.yml"),
                                            confenv.getActiveProfiles()[0]);
        confenv.getPropertySources().addFirst(datasource);
    } catch (final IOException e) {
        throw new RuntimeException("Failed to load datasource properties", e);
    }
}
我可以从Application.groovy使用调试器检查我的ApplicationContext,并确认我的
属性资源占位符配置器
存在并且已加载值。检查
applicationContext.environment.propertySources
它不在那里


我错过了什么

使用
属性资源占位符配置器
不会将属性添加到
环境
。在您的配置类的类级别上使用类似于
@PropertySource(“classpath:something.properties”)
的东西会将属性添加到
环境
,但遗憾的是,这不适用于
yaml
-文件

因此,您必须手动将从
yaml
文件读取的属性添加到
环境中。有一种方法可以做到这一点:

@Bean
public PropertySourcesPlaceholderConfigurer config(final ConfigurableEnvironment confenv) {
    final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    final YamlPropertiesFactoryBean yamlProperties = new YamlPropertiesFactoryBean();
    yamlProperties.setResources(new ClassPathResource("datasource.yml"));
    configurer.setProperties(yamlProperties.getObject());

    confenv.getPropertySources().addFirst(new PropertiesPropertySource("datasource", yamlProperties.getObject()));

    return configurer;
}
使用此代码,您可以使用以下两种方式之一注入属性:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = PropertiesConfiguration.class)
public class ConfigTest {

    @Autowired
    private Environment environment;

    @Value("${datasource.username}")
    private String username;

    @Test
    public void props() {
        System.out.println(environment.getProperty("datasource.username"));
        System.out.println(username);
    }
}
对于问题中提供的属性,这将打印“sa”两次

编辑:现在似乎并不需要
propertysourcesplaceconfigurer
,因此代码可以简化为以下内容,并且仍然生成相同的输出

@Autowired
public void config(final ConfigurableEnvironment confenv) {
    final YamlPropertiesFactoryBean yamlProperties = new YamlPropertiesFactoryBean();
    yamlProperties.setResources(new ClassPathResource("datasource.yml"));

    confenv.getPropertySources().addFirst(new PropertiesPropertySource("datasource", yamlProperties.getObject()));
}
编辑2:

我现在看到,您希望在一个文件中使用包含多个文档的yaml文件,并通过概要文件选择Spring引导样式。使用普通弹簧似乎是不可能的。因此,我认为您必须将yaml文件拆分为几个名为“datasource-{profile}.yml”的文件。然后,这应该会起作用(可能需要对多个配置文件进行更高级的检查,等等)

编辑3:

也可以使用SpringBoot的功能,而无需对项目进行完全转换(不过我还没有在真正的项目上尝试过)。通过向org.springframework.boot:springboot:1.5.9.RELEASE添加一个依赖项
,我能够让它使用单个
datasource.yml
和多个概要文件,如下所示:

---
spring:
  profiles: development

datasource:
  username: sa
  password:
  databaseUrl: jdbc:h2:mem:tests
  databaseDriver: org.h2.Driver
@Autowired
public void config (final ConfigurableEnvironment confenv) {
    final YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
    try {
        final PropertySource<?> datasource =
                yamlPropertySourceLoader.load("datasource",
                                            new ClassPathResource("datasource.yml"),
                                            confenv.getActiveProfiles()[0]);
        confenv.getPropertySources().addFirst(datasource);
    } catch (final IOException e) {
        throw new RuntimeException("Failed to load datasource properties", e);
    }
}
@Autowired
公共无效配置(最终可配置环境){
最终YamlPropertySourceLoader YamlPropertySourceLoader=新YamlPropertySourceLoader();
试一试{
最终属性源数据源=
yamlPropertySourceLoader.load(“数据源”,
新类路径资源(“datasource.yml”),
confenv.getActiveProfiles()[0]);
confenv.getPropertySources().addFirst(数据源);
}捕获(最终IOE例外){
抛出新的RuntimeException(“加载数据源属性失败”,e);
}
}

它可以工作-有些许。未考虑激活的弹簧配置文件,并且始终返回yml文件中的最后一个值。即使它包含在不同的弹簧轮廓块下。这仅仅是一个Spring boot的东西,还是应该工作呢?我认为这是Spring boot特有的。对于常规Spring,我认为将属性保存在单独的文件中更为常见。