spring Sping Boot 不加载application.yml配置

fnvucqvd  于 12个月前  发布在  Spring
关注(0)|答案(4)|浏览(173)

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

@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没有加载;
application-eho.yml文件:

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

root:
  name: EHO-BOT


应用程序使用args运行,但所有props均为null。未应用日志记录属性; sout:

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

--spring.profiles.active=eho

--spring.output.ansi.enabled=always

velaa5lx

velaa5lx1#

现在你应该用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

8i9zcol2

8i9zcol22#

试试这个方法:

遵循应用程序结构,如**:**

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

pdkcd3nj

pdkcd3nj3#

如果你正在使用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

relj7zay

relj7zay4#

添加
主类中的@PropertySource("classpath:application.yml")
看看下面的例子:
`

@SpringBootApplication
@EnableEurekaClient
@PropertySource("classpath:application.yml")
public class TestSpringApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestSpringApplication.class, args);
    }
}

`个

相关问题