当前位置:   article > 正文

spring boot 三种配置文件的三种注入数据方式_springboot 参数注入几种方式

springboot 参数注入几种方式
1.application.properties配置文件
  1. book.id=1
  2. book.name=我的老千生涯
  3. book.author=黑色枷锁

2.application.yml

  1. book:
  2. id: 2
  3. name: 我的老千江湖
  4. author: 黑色枷锁

3.application.yaml

  1. book:
  2. id: 3
  3. name: 新民间老八门
  4. author: 黑色枷锁

1.单属性注入@Value

  1. @Component
  2. public class Book {
  3. @Value("${book.id}")
  4. private Integer id;
  5. @Value("${book.name}")
  6. private String name;
  7. @Value("${book.author}")
  8. private String author;
  9. @Override
  10. public String toString() {
  11. return "Book{" +
  12. "id=" + id +
  13. ", name='" + name + '\'' +
  14. ", names='" + author + '\'' +
  15. '}';
  16. }
  17. public Integer getId() {
  18. return id;
  19. }
  20. public void setId(Integer id) {
  21. this.id = id;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public String getAuthor() {
  30. return author;
  31. }
  32. public void setAuthor(String author) {
  33. this.author = author;
  34. }
  35. }
  1. @RestController
  2. public class HelloController {
  3. @Autowired
  4. private Book book;
  5. @RequestMapping("/book")
  6. public Book book() {
  7. return book;
  8. }
  9. }
页面结果:{"id":1,"name":"我的老千生涯","author":"黑色枷锁"}

2.Environment

  1. import com.example.demo.pojo.Book;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.core.env.Environment;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class HelloController {
  8. @Autowired
  9. private Environment env;
  10. @RequestMapping("/book")
  11. public Book book() {
  12. System.out.println(env.getProperty("book.name"));
  13. return book;
  14. }
  15. }

控制台结果:我的老千生涯

3.@ConfigurationProperties

使用时属性名与配置文件一致时才会被注入

prefix = "book"要被读取的上级名没有不写

  1. @Component
  2. @ConfigurationProperties(prefix = "book")
  3. public class Book {
  4. private Integer id;
  5. private String name;
  6. private String author;
  7. @Override
  8. public String toString() {
  9. return "Book{" +
  10. "id=" + id +
  11. ", name='" + name + '\'' +
  12. ", names='" + author + '\'' +
  13. '}';
  14. }
  15. public Integer getId() {
  16. return id;
  17. }
  18. public void setId(Integer id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. public String getAuthor() {
  28. return author;
  29. }
  30. public void setAuthor(String author) {
  31. this.author = author;
  32. }
  33. }
  1. @RestController
  2. public class HelloController {
  3. @Autowired
  4. private Book book;
  5. @RequestMapping("/book")
  6. public Book book() {
  7. return book;
  8. }
  9. }

页面结果:{"id":1,"name":"我的老千生涯","author":"黑色枷锁"}

使用@ConfigurationProperties会出现

不影响运行

加入依赖以上问题会解决并配置文件会有注入值提示但有可能会报前言中不允许有内容

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-configuration-processor</artifactId>
  4. </dependency>

这个时候只需要以下操作就可以解决这个问题

1.删除.idea

2.重启idea会自动创建一个新的.idea

 

3.这个时候就可以正常运行了

 

 三种配置文件的读取方式注入方式一样

若三种配置文件同时存在读取优先级application.properties>application.yml>application.yaml

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/90526
推荐阅读
相关标签
  

闽ICP备14008679号