我用#CSDN#这个app发现了有技术含量的博客,小伙伴们求同去《SpringBoot @Value 读取配置,太强大了!》, 一起来围观吧 https://blog.csdn.net/zbw125/article/details/115744142?utm_source=app&app_version=4.5.8
1条回答 默认 最新
关注让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
问题:如何使用SpringBoot中的@Value注解读取配置文件? 回答:在SpringBoot中,可以使用@Value注解来读取配置文件中的属性。通过在需要注入属性的字段上加上@Value注解,并指定要读取的属性名,SpringBoot会自动将配置文件中对应的值注入到字段中。 示例代码如下:import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("${my.property}") private String myProperty; public String getMyProperty() { return myProperty; } }在配置文件(比如application.properties)中设置属性值:
my.property=myValue这样就可以通过@Value注解读取配置文件中的属性值,并使用该值。
解决 无用评论 打赏 举报