欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-09 18:58:14
...

@PropertySource的单独使用

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

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

自定义配置文件

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进行配置文件数据注入
注入成功!

@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;
    }
}

进行相同测试步骤
查看结果
@ConfigurationProperties配合@PropertySource进行配置文件数据注入
注入成功,无乱码!!!

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