Java Spring Boot 不加载 application.yml 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36761956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:39:34  来源:igfitidea点击:

Spring Boot doesn't load application.yml config

javaspringspring-boot

提问by yazabara

I have a simple main app:

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

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

With config:

使用配置:

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

And properties:

和属性:

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

I try to load config:

我尝试加载配置:

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

Path is correct. But yml isn't loaded;

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

application-eho.yml file:

应用程序-eho.yml 文件:

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

root:
  name: EHO-BOT

App runs with args, but all props null. Logging properties not aplied; sout:

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

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

--spring.profiles.active=eho

--spring.output.ansi.enabled=always

采纳答案by yazabara

For this moment you should use spring-boot.

此时你应该使用 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 {

        }
    }

Use webEnvironmet=false, and BannerMode=off (console application).

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

Docs, see https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-externalize-configuration

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

回答by Riddhi Gohil

Try this way :

试试这个方法:

Follow application structure like :

遵循应用程序结构,如

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

Application.javacontent

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);
    }

}

application-eho.ymlfile:

应用程序-eho.yml文件:

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

root:
  name: EHO-BOT