Spring configuration files inject list, map, and byte streams.

The list into

The properties file

User id = 3, 3242232

Use spring EL expressions

@Value(“#{‘${user.id}’.split(‘,’)}”)

private List<Integer> list;
Copy the code

Yaml files

Configure the array mode in the YML configuration file

number:
  arrays: 
    - One
    - Two
    - Three
Copy the code

@Value(“${number.arrays}”)

private List list

Although the net all says, so can inject, I personally practiced, affirmation cannot. Will be thrown under Caused by: Java. Lang. IllegalArgumentException: Could not resolve placeholder ‘number.arrays’ in value “${number.arrays}” To inject, you must use @ConfigurationProperties

@ConfigurationProperties(prefix = "number")
public class AgentController {

    private List arrays;
    public List getArrays(a) {
        return arrays;
    }

    public void setArrays(List arrays) {
        this.arrays = arrays;
    }
    @GetMapping("/s")
    public List lists(a){
        return arrays;
    }
Copy the code

Don’t want to be so troublesome, you can write it like a properties file and use an EL expression

number:
  arrays: One,Two,Three
Copy the code

@Value(“#{‘${number.arrays}’.split(‘,’)}”)

private List arrays;
Copy the code

Injection file stream

    @Value("classpath: application.yml")
    private Resource resource;
   
    / / placeholder
    @Value("${file.name}")
    private Resource resource2;

    @GetMapping("/s")
    public String lists(a) throws IOException {
        return IOUtils.toString(resource.getInputStream(),"UTF-8");
    }
Copy the code

From the classpath to load application. Yml file will be injected into the org. Springframework. Core. IO. The Resource, you can use getInputStream () method for flow. It is much more elegant and simple than using the classloader to get the path and then load the file.

Map Key Value Injection

properties

resource.code.mapper={x86:”hostIp”}

@Value(“#{${resource.code.mapper}}”)

private Map<String, String> mapper;
Copy the code

Successful injection

yaml

In yamL files, you can’t inject a Map instance with @Value. Instead, you can use @ConfigurationProperties.

@ConfigurationProperties(prefix = "blog")
public class AgentController {

    private Map website;

    public Map getWebsite(a) {
        return website;
    }

 public void setWebsite(Map website) {
        this.website = website;
    }

    @GetMapping("/s")
    public String lists(a) throws IOException {
        return JsonUtil.toJsonString(website);
    }
Copy the code

The configuration file

blog:
  website:
    juejin: https://juejin.cn
    jianshu: https://jianshu.com
    sifou: https://segmentfault.com/
Copy the code

@ConfigurationProperties is much better than @Value. You can inject not only lists and maps, but also object properties, static inner class properties, In the Spring the Boot Redis module org. Springframework. Boot. Autoconfigure. Data. Redis. RedisProperties.

The difference between

The difference between @ConfigurationProperties @Value
type Various copy type attribute maps, inner classes Only simple attributes are supported
SpEl expression Does not support support
JSR303 data verification support Does not support
function Batch injection of a column attribute Single attribute injection