SpringBoot慕课学习-SpringBoot开发常用技术整合-资源文件属性配置

作用:资源文件中的配置属性映射到实体类属性

1.引入包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

2. 创建一个properties文件resource.properties

opensource.name=hello
opensource.website=imooc.com
opensource.language=java

3. 创建Resource.java

复制代码
//@Configuration 声明使用配置文件
@Configuration
@ConfigurationProperties(prefix="opensource")
@PropertySource(value="classpath:resource.properties")
public class Resource {

    private String name;
    private String website;
    private String language;

//getter.setter

}
复制代码

4. 需要使用的地方使用 自动装配

@Autowired
private Resource resource;

 

posted @   BigOrang  阅读(258)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示