@ConfigurationProperties配合@PropertySource进行配置文件数据注入

@PropertySource的单独使用

单独使用时需配合@value进行注入

在这里插入图片描述

自定义配置文件

demo.list=1,2,3,4
demo.name=zhangsan
@PropertySource属性解释
value为配置文件路径,encoding 为编码格式
ignoreResourceNotFound为当文件不存在是否报错,false为报错

代码

package com.channelsoft.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@PropertySource(value = {"/test/application.property"}, encoding = "UTF-8",
        ignoreResourceNotFound=false)
public class Test {

    @Value("${demo.list}")
    private String[] list;

    @Value("${demo.name}")
    private String name;

    @Override
    public String toString() {
        return "Test{" +
                "list=" + Arrays.toString(list) +
                ", name='" + name + '\'' +
                '}';
    }

    public String[] getList() {
        return list;
    }

    public void setList(String[] list) {
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

编写一个controller进行测试

package com.channelsoft.controller;

import com.channelsoft.utils.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @Autowired
    private Test test;

    @RequestMapping("/demo")
    @ResponseBody
    public String test(){
        System.out.println(test.toString());
        return test.toString();
    }

}

查看结果
在这里插入图片描述
注入成功!

@ConfigurationProperties配合@PropertySource进行使用

代码

package com.channelsoft.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@PropertySource(value = {"/test/application.property"}, encoding = "UTF-8",
        ignoreResourceNotFound=false)
@ConfigurationProperties(prefix = "demo")
public class Test {

    private String[] list;

    private String name;

    @Override
    public String toString() {
        return "Test{" +
                "list=" + Arrays.toString(list) +
                ", name='" + name + '\'' +
                '}';
    }

    public String[] getList() {
        return list;
    }

    public void setList(String[] list) {
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

进行相同测试步骤
查看结果
在这里插入图片描述
注入成功,无乱码!!!

<!‐‐导入配置文件处理器,配置文件进行绑定就会有提示‐‐>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
</dependency>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值