springboot怎么获取application.yml的值

   2024-10-01 7530
核心提示:在Spring Boot项目中,可以通过使用@Value注解来获取application.yml中的值。首先,在Spring Boot的配置类中使用@PropertySource

在Spring Boot项目中,可以通过使用@Value注解来获取application.yml中的值。

首先,在Spring Boot的配置类中使用@PropertySource注解来指定application.yml的位置,例如:

@Configuration@PropertySource("classpath:application.yml")public class AppConfig {}

然后在需要获取值的地方,使用@Value注解来注入对应的值,例如:

@Componentpublic class MyComponent {    @Value("${my.property}")    private String myProperty;        public String getMyProperty() {        return myProperty;    }}

在application.yml中定义的my.property属性可以通过@Value注解注入到MyComponent组件中。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号