博主
258
258
258
258
专辑

第六节 自定义配置文件以及使用配置类

亮子 2021-05-10 15:14:22 5594 0 0 0

1、默认配置类

在上一节中,我们在配置中设置的参数,都是通过@Value注解来读取的,这样对于少量的参数配置是比较方便的。但是如果想要改变属性值或者参数比较多,就有点不太方便了,因此我再介绍一种通过配置类来读取配置的方法。

1)、配置文件内容

server.port=9092
spring.application.name=demo-new

server.servlet.session.timeout=120m

## 自定义属性
# 数字和字符型
site.name="hello"

# map类型
site.maps={name:'dev-server',ip:'192.168.1.100',port:'9999'}

# List类型
site.whites=1,2,3,4

# 数组类型
site.names=david,lisa,alex,lucy

# Set类型
site.ages=66,99,88,34

# 占位符
site.age=${random.int[1024,65536]}
site.uuid=梅超风${random.uuid}

2)、配置类代码

package com.shenmazong.demonew.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Data
@Component
@ConfigurationProperties(prefix = "site")
public class SiteConfig {
    private Integer serverPort;

    private String serverName;

    private String name;

    private Map<String,String> maps;

    private List whites;

    private String[] names;

    private Set ages;

    private Integer age;

    private String uuid;
}

3)、控制类输出

package com.shenmazong.demonew.controller;

import com.shenmazong.demonew.config.SiteConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @Autowired
    SiteConfig siteConfig;

    @GetMapping(value = "/")
    public Object index() {
        System.out.println(siteConfig);
        return siteConfig;
    }
}

2、自定义配置类

1)、site.properties配置内容

除了在默认的application文件进行属性配置,我们也可以自定义配置文件,例如新建 site.properties ,配置内容如下:

# site.properties

## 自定义属性
# 数字和字符型
www.name=www.shenmazong.com

# map类型
www.maps.name=dev-server
www.maps.ip=192.168.1.100
www.maps.port=9999

# List类型
www.whites=1,2,3,4

# 数组类型
www.names=david,lisa,alex,lucy

# Set类型
www.ages=66,99,88,34

# 占位符
www.age=${random.int[1024,65536]}
www.uuid=梅超风${random.uuid}

2)、自定义配置类

package com.shenmazong.demonew.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Data
@Service
@ConfigurationProperties(prefix = "www")
@PropertySource(value = {
        "classpath:site.properties",
}, encoding = "utf-8")
public class ShenmaConfig {

    private String name;

    private Map maps;

    private List whites;

    private String[] names;

    private Set ages;

    private Integer age;

    private String uuid;
}

在上面的配置文件编写中,maps的写法稍微有点变化,如果按照之前的写法,这个变量无法绑定。

3)、自定义配置文件的设置

根据上面代码,可以在配置类上定义自定义配置文件的名字和编码。
如果你需要在更早一点引入,则可以在启动类上进行引入自定义文件的配置,具体代码如下:

package com.shenmazong.demonew;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

//手动加载自定义配置文件
@PropertySource(value = {
        "classpath:site.properties"
}, encoding = "utf-8")
@Component
@SpringBootApplication
public class DemoNewApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoNewApplication.class, args);
    }


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

//    @Value("#{${www.maps}}")
//    private Map<String,String> siteMaps;

    @Value("${www.whites}")
    private List siteWhites;

    @Value("${www.names}")
    private String[] siteNames;

    @Value("${www.ages}")
    private Set siteAges;

    @Value("${www.age}")
    private Integer siteAge;

    @Value("${www.uuid}")
    private String siteUuid;

    @Bean
    void index(){
        System.out.println("siteName="+siteName);
//        System.out.println("serverMaps="+siteMaps);
        System.out.println("siteWhites="+siteWhites);
        System.out.println("siteNames="+siteNames);
        System.out.println("siteAges="+siteAges);
        System.out.println("siteAge="+siteAge);
        System.out.println("siteUuid="+siteUuid);
    }
}

上面的map读取有问题,暂时先注释,如果有调通的网友可以给我发私信,我修改一下这个博文,这里我先谢谢了。