Spring Boot不加载application.yml config

时间:2016-04-21 06:58:52

标签: java spring spring-boot

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

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

使用config:

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

和属性:

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

我尝试加载config:

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

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

application-eho.yml文件:

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

root:
  name: EHO-BOT

App使用args运行,但所有props都为null。记录属性不适用; SOUT:

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

--spring.profiles.active=eho

--spring.output.ansi.enabled=always

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

尝试这种方式:

  

遵循

等应用结构
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);
    }

}
  

application-eho.yml 文件:

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

root:
  name: EHO-BOT

答案 2 :(得分:0)

如果您使用的是 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