gpt4 book ai didi

java - Spring Boot 不加载 application.yml 配置

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:01:36 27 4
gpt4 key购买 nike

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

@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 运行,但所有 props 都为 null。未应用日志记录属性;苏:

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

--spring.profiles.active=eho

--spring.output.ansi.enabled=always

最佳答案

尝试这种方式:

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

@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:

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

root:
name: EHO-BOT

关于java - Spring Boot 不加载 application.yml 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36761956/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com