繁体   English   中英

Spring Boot 不加载 application.yml 配置

[英]Spring Boot doesn't load application.yml config

我有一个简单的主应用程序:

@Configuration
    @EnableAutoConfiguration
    @ComponentScan(basePackages = "dreamteam.eho")
    @Import({EhoConfig.class})
    public class MainApp implements CommandLineRunner, ApplicationContextAware {

使用配置:

@Configuration
@EnableConfigurationProperties({RootProperties.class})
public class EhoConfig {
}

和属性:

@ConfigurationProperties("root")
public class RootProperties {
    private String name;

我尝试加载配置:

--spring.config.location=file:///E:/.../eho-bot/props/ --spring.profiles.active=eho

路径正确。 但是 yml 没有加载;

应用程序-eho.yml 文件:

logging:
  file: D:/java/projects/telegram-bots/eho.log
  level:
    dreamteam.eho: INFO
    org.springframework: DEBUG

root:
  name: EHO-BOT

应用程序使用 args 运行,但所有道具为空。 未应用日志属性; 南:

--spring.config.location=file:///E:.../eho-bot/props/

--spring.profiles.active=eho

--spring.output.ansi.enabled=always

此时你应该使用 spring-boot。

    @SpringBootApplication
    public class ReTestsApplication implements CommandLineRunner {

        public static void main(String[] args) {
            SpringApplication application = new SpringApplication(ReTestsApplication.class);
            application.setWebEnvironment(false);
            application.setBannerMode(Banner.Mode.OFF);
            application.run(args);
        }

        public void run(String... args) throws Exception {

        }
    }

使用 webEnvironmet=false 和 BannerMode=off(控制台应用程序)。

文档,见https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-externalize-configuration

试试这个方法:

遵循应用程序结构,如

App
└── src
|    ├── main
|         ├── java
|         │     └── <base-package>
|         │               └── Application.java (having public static void main() method)
|         │
|         ├── resources
|                ├─── application-eho.yml
|
├──── pom.xml

Application.java内容

@SpringBootApplication
@RestController
public class Application {

    public static void main(String[] args) {
        System.setProperty("spring.config.name", "application-eho");
        SpringApplication.run(Application.class, args);
    }

}

应用程序-eho.yml文件:

logging:
  file: D:/java/projects/telegram-bots/eho.log
  level:
    dreamteam.eho: INFO
    org.springframework: DEBUG

root:
  name: EHO-BOT

如果您使用的是PropertySource注释,则这是不可能的。 默认情况下它不支持 yml 文件类型。 使用PropertySourceFactory如下

    public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) 
      throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());

        Properties properties = factory.getObject();

        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }
}

然后,在您的属性源注释中引用相同的内容,如下所示:

@PropertySource(value = "classpath:foo.yml", factory = YamlPropertySourceFactory.class)

来源: PropertySourceFactory

暂无
暂无

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

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