酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Spring Boot 如何读取 application.yml 文件中 profile 的值?示例代码揭秘!

在Spring Boot中,可以通过`Environment`接口来读取`application.yml`文件中的profile的值。 以下是一个示例代码,展示如何读取`application.yml`文件中的`spring.profiles.active`的值: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.env.Environment; @SpringBootApplication public class Application { @Autowired private Environment environment; public static void main(String[] args) { SpringApplication.run(Application.class, args); } public void printActiveProfile() { String activeProfile = environment.getProperty("spring.profiles.active"); System.out.println("Active profile: " + activeProfile); } } ``` 在上面的例子中,通过注入`Environment`接口,并使用`getProperty`方法来获取`spring.profiles.active`的值。 然后,可以在应用程序中的其他位置使用`printActiveProfile`方法来打印当前的active profile。 上述代码中使用了Spring Boot的注解@SpringBootApplication来标记主类。 ######[AI写代码神器 | 741点数解答 | 2024-05-22 09:40:48]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]