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;

猜你喜欢

转载自www.cnblogs.com/bigorang/p/9591234.html