Spring Properties整理

2020-06-13  本文已影响0人  大帅的大帅

spring 配置及配置文件,是学习spring的基础知识,那么,这篇文档我们先了解下spring 的配置写法,以及如何加载配置的。

解析 application.yaml

YAML语法

YAML基本语法:

YAML支持的三种数据结构。

YMAL写的注意事项

字面量:

以key: value来表示,value前必须带一个空格。

port: 8080

对象、Map

第一种写法

UsersInfo:
  name: DerikLi
  email: sodingli@hotmail.com

第二种写法
大括号的方式,还有逗号

UsersInfo: {
 name: DerikLi,
 email: sodingli@hotmail.com
 }

数组

数组用 “-”表示。

UsersInfo:
   - name
   - name
   - age

当然,还有一种,冒号不要丢失

UsersInfo: [name,name,age]

配置映射

Spring的配置通过配置文件进行映射。分为两种方式,一种是 @ConfigurationProperties 读取属性,一种是@Value这种读取单个的值。

通过 ConfigurationProperties 整块映射属性值

类文件为

@ConfigurationProperties("monapi")
@Data
public class MonitorApiproperties {

    private String  uris;
    private int interval;
    private String hostname;
    private List<String> xml = new ArrayList<>(); //指定获取多个配置
}
monapi:
  uris: http://monitor.com:8080 
  interval: 10
  hostname: monapi-test-vm01
  xml: classpath:esmapper/*/*.xml,,classpath:etcdmapper/*.xml

这是一种通过路径monapi 映射方式。

这里如果

支持对 通过类加载的方式,对配置的值进行映射。

加载配置属性

映射完成后,我们还得让Spring 能扫描的到。 那么又可以用到两种方式。
让 Spring 知道我们的 @ConfigurationProperties 类存在,以便将其加载到应用程序上下文中。

第一种,就是让自定义的类,注册成为一个启动类的配置,然后的加载方式。@EnableConfigurationProperties({MonitorApiproperties.class})
可以在启动类了,就是主入口类,加载配置

@SpringBootApplication
@EnableConfigurationProperties({MonitorApiproperties.class})//here
public class Application {

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

第二种,可以直接在 ConfigurationProperties @ComponentScan 注解参数。

@Component //加上组件类
@ConfigurationProperties("monapi")
public class MonitorApiproperties {

    private String  uris;
    private int interval;
    private String hostname;
    private List<String> xml = new ArrayList<>(); //指定获取多个配置
    
    public List<String> getUris() {
        return uris;
    }

    public void setUris(List<String> uris) {
        this.uris = uris;
    }

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    
    
}

还可以通过bean 生成配置。 也可以直接用了,当然如果是bean 加载的话,就不用在
MonitorApiproperties 注释 @Component 了。

@Configuration
public class MonPropertiesConfigration {

    @Bean
    public MonitorApiproperties monitorApiproperties(){
        return new MonitorApiproperties();
    }
    
}

获取参数值,用于构建客户端

调用,一般都是在 Configuration 去取参数,就可以直接用了。

@Configuration
public class MonPropertiesConfigration {

    @Bean
    public Client client(MonitorApiproperties configraton) {
        try {

            PreBuiltTransportClient client = new PreBuiltTransportClient(configraton.getUris()[0]);
            return client;
        } catch (Exception e) {
            throw new RuntimeException("build transport client error", e);
        }
    }


}

通过 value 映射属性值

这就比较简单了,直接获取值就行了,只能一个值一个值的获取。

@Component
public class PrometheusRepostitoryImpl implements PrometheusRepostitory {

@Value("${spring.prometheus.host}")
 private String host;

@Value("${spring.prometheus.port}")
private String port;

}

Spring 配置文件加载顺序

配置文件加载位置和顺序,springboot启动会扫描一下位置的配置文件作为springboot的默认配置文件。

SpringBoot也可以从以下位置加载配置;优先级从高到低,高优先级的配置覆盖低优先级的配置,所有配置形成互补配置。

多配置文件读取规则

有时候需要配置多个配置文件,一个主配置文件,application.yml和开发配置文件,和生产环境配置。
application.yml
application-dev.yml
application-prod.yml

我们通过在主配置文件用

spring.profile.active = dev(prod) #来选择需要加装的配置文件。

配置命名
而在 application-dev.yml 和 application-prod.yml ,为了能找到 dev(prod) 的标签,我们通过 ,配置名称方式,来读取。

spring:
    profiles: dev #给文档命名为dev

可以使用spring.profile.active来进行切换。

也可以通过命令行指定配置文件

Copyjava –jar springboot02-0.0.1-SHAPSHOT.jar –spring.profiles.active=dev
上一篇 下一篇

猜你喜欢

热点阅读