SpringBoot中的配置绑定

配置绑定是SpringBoot其中一个底层功能,SpringBoot把配置绑定的过程变得更加简单,

传统java将常用的配置放到配置文件properties中,之后将这些配置绑定到javabean中。
java代码:

 1 public class getProperties {
 2      public static void main(String[] args) throws FileNotFoundException, IOException {
 3          Properties pps = new Properties();
 4          pps.load(new FileInputStream("a.properties"));
 5          Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
 6          while(enum1.hasMoreElements()) {
 7              String strKey = (String) enum1.nextElement();
 8              String strValue = pps.getProperty(strKey);
 9              System.out.println(strKey + "=" + strValue);
10              //封装到JavaBean。
11          }
12      }
13  }

而SpringBoot只需要一个@ConfigurationProperties注解就可实现
在配置文件中:

在Car类中添加注解@Component添加为容器中的组件,然后通过@ConfigurationProperties进行绑定

 

posted on 2022-09-11 15:31  风中明月  阅读(198)  评论(0编辑  收藏  举报