Spring Boot 资源文件配置(1)------- 读取资源文件内容
例如,读取该资源文件中的内容一、可以写个实体类,来对应资源文件中的属性
/**
* 读取资源文件,实体类
*/
//代表Resources会引用资源文件
@Configuration
//前缀,映射时只会映射后面的内容
@ConfigurationProperties(prefix = "com.imooc.opensource")
//资源文件地址
@PropertySource(value = "classpath:resources.properties")
public class Resources {
private String name;
private String website;
private String language;
----构造方法、setter()/getter()-----
}
二、引入依赖包
<!--2019-5-14 资源文件读取 configuration-->
<!-- application.xml配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
三、写个请求,来获取配置文件中的内容
@RestController
public class HelloController {
@Autowired
private Resources resources; //会从资源文件中取到值
@RequestMapping(value="/getsources",method = RequestMethod.GET)
public CustomJsonResult getResources(){
Resources bean = new Resources();
BeanUtils.copyProperties(resources,bean);
return CustomJsonResult.build(200,"success",bean);
}
}
四、运行,结果显示。可以看到数据取到了