简体   繁体   English

Spring Boot 不加载 application.yml 配置

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

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.应用程序使用 args 运行,但所有道具为空。 Logging properties not aplied;未应用日志属性; sout:南:

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

--spring.profiles.active=eho

--spring.output.ansi.enabled=always

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

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.java content 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.yml file:应用程序-eho.yml文件:

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

root:
  name: EHO-BOT

if you are using PropertySource annotation then it is not possible.如果您使用的是PropertySource注释,则这是不可能的。 Be default it doesn't support yml file types.默认情况下它不支持 yml 文件类型。 use PropertySourceFactory as below使用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);
    }
}

Then, refer to the same in your property source annotation as follows:然后,在您的属性源注释中引用相同的内容,如下所示:

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

Source: PropertySourceFactory来源: PropertySourceFactory

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

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