SpringBoot中读取配置文件的方式有五种:使用@Value读取配置文件。使用@ConfigurationProperties读取配置文件。使用Environment读取配置文件。使用@PropertySource读取配置文件。使用native方式读取配置文件。它们的具体使用方法如下。为了方便测试,我们在SpringBoot配置文件application.properties中添加如下内容:profile.name=SpringBootProfileprofile.desc=SpringBootProfileDesc.1。使用@Value读取配置文件使用@Value可以读取单个配置项,如下代码所示:@SpringBootApplicationpublicclassDemoApplicationimplementsInitializingBean{@Value("${profile.name}")privateStringname;publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}@OverridepublicvoidafterPropertiesSet()throwsException{System.out.println("MyProfileName:"+name);}}上面程序的执行结果如下图所示:2.使用@ConfigurationProperties读取配置文件@ConfigurationProperties和@Value的使用方式略有不同。@Value读取单个配置项,而@ConfigurationProperties读取一组配置项。我们可以使用@ConfigurationProperties加上实体类来读取一组Configuration项,如下代码所示:组件@ConfigurationProperties(prefix="profile")@DatapublicclassProfile{privateStringname;privateStringdesc;}其中prefix表示读取一组配置项的根名,相当于Java中的类名,最后注入这个配置类放入某个类中即可使用,如下代码所示:@SpringBootApplicationpublicclassDemoApplicationimplementsInitializingBean{@AutowiredprivateProfileprofile;publicstaticvoidmain(String[]args){args);}@OverridepublicvoidafterPropertiesSet()throwsException{System.out.println("ProfileObject:"+profile);}}上面程序的执行结果如下图所示:3.使用Environment读取配置文件环境是SpringCore读取配置文件的一个类,使用@Autowired将这个类注入类中可以使用它的getProperty方法获取一个配置项的值,如下代码所示:importorg.springframework。beans.factory.InitializingBean;导入org.springframework.beans.factory.annotation.Autowired;导入org.springframework.boot.SpringApplication;导入org.springframework.boot.autoconfigure.SpringBootApplication;导入org.springframework.core.env.Environment;@SpringBootApplicationpublicclassDemoApplicationimplementsInitializingBean{@AutowiredprivateEnvironment环境;publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}@OverridepublicvoidafterPropertiesSet()throwsSystem.Exceptionout.println("ProfileName:"+environment.getProperty("profile.name"));}}上面程序的执行结果如下图所示:4.使用@PropertySource读取配置文件。@PropertySource注解可以用来指定读取一个配置文件,比如指定读取application.properties配置文件的配置内容。具体实现代码如下:@SpringBootApplication@PropertySource("classpath:application.properties")publicclassDemoApplicationimplementsInitializingBean{@Value("${profile.name}")privateStringname;publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}@OverridepublicvoidafterPropertiesSet()throwsException{System.out.println("Name:"+name);}}上面程序的执行结果如下图所示:中文乱码如果配置文件中有中文乱码,可以通过指定来解决中文乱码问题编码格式,具体实现如下:@PropertySource(value="dev.properties",encoding="utf-8")注意事项@PropertySource注解默认只支持读取属性格式配置文件5.使用native方式读取配置文件我们也可以使用最原始的方式读取配置文件,Properties对象,如下代码所示:springframework.boot.SpringApplication;导入org.springframework.boot.autoconfigure.SpringBootApplication;导入java.io.IOException;导入java.io.InputStreamReader;导入java.nio.charset.StandardCharsets;导入java.util.Properties;@SpringBootApplication公共类DemoApplication实现InitializingBean{publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}@OverridepublicvoidafterPropertiesSet()throwsException{Propertiesprops=newProperties();尝试{InputStreamReaderinputStreamReader=newInputStreamReader(this.getClass().getClassLoader().getResourceAsStream("application.properties"),StandardCharsets.UTF_8);props.load(inputStreamReader);}赶上(IO异常e1){System.out.println(e1);}System.out.println("属性名称:"+props.getProperty("profile.name"));}}上述程序的执行结果如下图所示:总结SpringBoot中读取配置文件的方式有五种:使用@Value读取配置文件使用@ConfigurationProperties读取配置文件。使用@PropertySource读取配置文件。使用Environment读取配置文件。使用native方式读取配置文件。最常用的是前三个。如果读取某个配置项,可以使用@Value。如果读取一组配置项,可以使用@ConfigurationProperties。如果要指定读取特定的配置文件,可以使用@PropertySource来指定。判断是非在自己,名誉在别人,得失在人数。公众号:Java面试真题分析面试合集:https://gitee.com/mydb/interview
