SpringBoot学习,配置文件Config—读取指定文件和读取外部配置文件

本文介绍了在SpringBoot中如何使用@PropertySource读取指定的properties文件,以及@ImportResource导入外部XML配置文件。SpringBoot推荐使用注解方式进行配置,以实现更加灵活的组件管理。
摘要由CSDN通过智能技术生成

@PropertySource读取指定文件

@PropertySource加载指定的配置文件,只能用于properties文件。经测算对yml文件无效
在application.properties是全局配置文件,而我们在系统运行的时候可能会增加一些其他的配置,与Spring Boot无关的配置,不建议放在全局配置文件中,这就要用到@PropertySource@PropertySource注解要配合@ConfigurationProperties注解一起使用。
@PropertySource(value = {})可以使用数组的形式,加载多个配置文件

实体:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
 * @ClassName Person
 * @Date 2019/7/11 16:57
 * @Description
 */
@Component
@PropertySource(value = "classpath:person.properties")
@ConfigurationProperties(prefix = "person")
public class Person {
   
    private String surname;
    private String name;
    private Integer age;
    private Date birth;
    private boolean boss;
    public String getSurname() {
   
        return surname;
    }
    public void setSurname(String surname) {
   
        this.surname = surname;
    }
    public String getName() {
   
        return name;
    }
    public void setName(String name) {
   
        this
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值