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项,如下代码所示:Component@ConfigurationProperties(prefix="profile")@Datapublic类简介{私有字符串名称;privateStringdesc;}其中prefix表示读取一组配置项的根名,相当于Java中的类名,最后将这个配置类注入到某个类中使用,如图以下代码:@SpringBootApplicationpublicclassDemoApplicationimplementsInitializingBean{@AutowiredprivateProfileprofile;publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}@OverridepublicvoidafterPropertiesSet()throwsException{System.out.println("ProfileObject:"+profile);}}上面程序的执行结果如下图所示:3.使用Environment读取配置文件Environment是SpringCore中一个用来读取配置文件的类,使用@Autowired将这个类注入类中可以使用它的getProperty方法获取配置项的值,如下代码所示:org.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.core.env.Environment;@SpringBootApplicationpublicclassDemoApplication实现InitializingBean{@AutowiredprivateEnvironment环境;publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}@OverridepublicvoidafterPropertiesSet()throwsException{System.out.println("ProfileName:"+environment.getProperty("profile.name"));}}上面程序的执行结果如下图所示:4.使用@PropertySource读取配置文件@PropertySource注解可以用来指定读取某个Configuration文件,比如指定读取配置内容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注解默认只支持properties格式配置文件的读取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);}赶上(IOExceptione1){System.out.println(e1);}System.out.println("属性名称:"+props.getProperty("profile.name"));}}上述程序的执行结果如下图所示:总结SpringBoot中读取配置文件的方式有五种:使用@Value读取配置文件使用@ConfigurationProperties读取配置文件。使用@PropertySource读取配置文件。使用Environment读取配置文件。使用native方式读取配置文件。最常用的是前三个。如果读取某个配置项,可以使用@Value。如果读取一组配置项,可以使用@ConfigurationProperties。如果要指定读取特定的配置文件,可以使用@PropertySource来指定。
