在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组件中。


