springboot读取配置文件方式

一、直接赋值

配置文件application.properties:

#随机值属性
my.number.a=${random.value}
my.number.b=${random.int}
my.number.c=${random.int(1,100)}
my.number.uid=${random.uuid}
my.number.myfriend=${myobject.name}

读取配置:

@Component
@Data
@ToString
public class MyNumber {
    @Value("${my.number.a}")
    private String a;
    @Value("${my.number.b}")
    private int b;
    @Value("${my.number.c}")
    private int c;
    @Value("${my.number.uid}")
    private String uid;
    @Value("${my.number.myfriend}")
    private String myfriend;

}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Dur5Course2SpringbootApplication.class)
class Dur5Course2SpringbootApplicationTests {
    @Autowired
    private MyNumber myNumber;
    @Test
    void myObject(){
        System.out.println(myNumber);
    }

结果:
在这里插入图片描述

二、通过 PropertySource注解读取自定义配置文件

stu.properties:

#配置学生对象的初始化信息
student.sid=2
student.sname=张三
student.score=99.5f

读取配置:

@Component
@Data
@ToString
@PropertySource(value = "classpath:stu.properties",encoding = "utf-8") //创建该bean的时候读取配置文件
public class Student {
    @Value("${student.sid}")
    private int sid;
    @Value("${student.sname}")
    private String sname;
    @Value("${student.score}")
    private float score;
}

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Dur5Course2SpringbootApplication.class)
class Dur5Course2SpringbootApplicationTests {
    @Autowired
    private Student student;
    @Test
    public void test(){
        System.out.println(student);
    }

结果:
在这里插入图片描述

三、在spring工厂启动之前,通过全局环境变量env预先加载配置:通过运行自定义的EnvirionmentProcessor类。(加载时间在springboot加载之前)

在resource下面建一个META-INF目录:
在该目录下建立一个spring.factories文件:

org.springframework.boot.env.EnvironmentPostProcessor=com.wance.processor.MyEnvironmentPostProcessor

在processor目录下建立一个MyEnvironmentPostProcessor类:

/**
 自定义环境处理,在运行SpringApplication之前加载任意配置文件到Environment环境中
 */
public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor {

    //Properties对象
    private final Properties properties = new Properties();

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment,SpringApplication application) {
        System.out.println("通过processor加载自定义配置");
        //自定义配置文件
        String[] profiles = {
                "stu.properties"
        };

        //循环添加
        for (String profile : profiles) {
            //从classpath路径下面查找文件
            Resource resource = new ClassPathResource(profile);
            //加载成PropertySource对象,并添加到Environment环境中
            environment.getPropertySources().addLast(loadProfiles(resource));
        }
    }

    //加载单个配置文件
    private PropertySource<?> loadProfiles(Resource resource) {
        if (!resource.exists()) {
            throw new IllegalArgumentException("资源" + resource + "不存在");
        }
        try {
            //从输入流中加载一个Properties对象
            properties.load(new InputStreamReader(resource.getInputStream(),"utf-8"));
            return new PropertiesPropertySource(resource.getFilename(), properties);
        }catch (IOException ex) {
            throw new IllegalStateException("加载配置文件失败" + resource, ex);
        }
    }
}

实体类:


@Component
@Data
@ToString
public class Student {
    @Value("${student.sid}")
    private int sid;
    @Value("${student.sname}")
    private String sname;
    @Value("${student.score}")
    private float score;
}

测试:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java后端指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值