spring-in-action-day04-配置属性 @ConfigurationProperties

1.Spring环境抽象的概念

2.怎么配置属性。在application.yml举例一些常用的配置

3.自定义配置属性@ConfigurationProperties(prefix = "xx.xxxx")

3.1在类上使用

3.2在方法上使用

  1. 配置属性元数据-即给配置属性加注释说明
  2. 使用profile配置多套配置属性,在不同的环境下根据需要切换配置属性
    1. 怎么定义多个profile
    2. 激活profile 设定配置类下哪些bean在特定的profile生效时才去创建(不设置是不论什么profile生效都自动创建)

:Spring环境抽象

Spring环境抽象:它时各种配置属性的一站式服务。它抽取了原始的属性,这样需要这些属性的bean就可以从spring本身获取了。配置属性也就是bean的属性值

Spring环境会拉取多个属性源:

  1. JVM系统属性
  2. 操作系统环境变量
  3. 命令行参数
  4. 应用属性配置文件(比如说:application.yml文件)

Spring环境抽象:我的理解,他会去读取上述的配置保存起来,在spring需要这些相关配置的时候取出来使用。

二:配置属性

以下例举了几个配置(application.yml文件中)

 1.几个常见配置实例

  1)数据源

spring:

  datasource:

    url: jdbc:mysql://localhost:3306/text?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC

    username: root

    password: 573875306

    driver-class-name: com.mysql.jdbc.Driver

    2)嵌入式服务,下面例举的是配置HTTPS

  #端口号

  server.port: 8443

  #你生成的证书名字

  server.ssl.key-store: E:work
ave	omcat.keystore

  #密钥库密码

  server.ssl.key-store-password: duan123

  server.ssl.keyStoreType: JKS

  server.ssl.keyAlias: tomcat

   3)配置日志

  默认情况下springbot通过logback配置日期,日志会议INFO级别写入控制台,在此基础上,我们可以做一些配置

  logging:

  path:/var/logs         //path 和 file 属性组成日志文件

  file:TacoCioud.log

  level: //配置级别 ,不如说想把root配置为WARN,把security配置为DEBUG(可以分区块来定义不同的级别)

  root:WARN

  org.springframework.security:DEBUG

 2.使用特定的属性值,相当于调用其他属性值

  比如我们想设置一个 greeting.welcom的属性,而这个属性的值是另一个属性spring.application.name的值

  Greeting:

  welcom: ${spring.application.name}

 3.创建自己的配置属性@ConfigurationProperties

  正如上面所说的,配置属性也就是bean的属性值。我们在spring的抽象环境中配置这些属性。spring上下文会在创建bean是自动装配。但是还没有讲我们自己如果想要消费配置属性,即手动给bean赋值配置属性,应该怎么操作。

  为了支持配置属性注入,springboot提供了注解@ConfigurationProperties

  Spring源码中大量使用了ConfigurationProperties注解,比如server.port就是由该注解获取到的,通过与其他注解配合使用,能够实现Bean的按需配置。该注解有一个prefix属性,通过指定的前缀,绑定配置文件中的配置,该注解可以放在类上,也可以放在方法上

  3.1@ConfigurationProperties作用与方法。比较常见的就是配置读写分离的场景。

  当将该注解作用于方法上时,如果想要有效的绑定配置,那么该方法需要有@Bean注解,且方法所属Class需要有@Configuration注解。
  简单一句话概括就是:Sring的有效运行是通过上下文(Bean容器)中Bean的配合完成的,Bean可以简单理解成对象,有些对象需要指定字段内容,那么这些内容我们可以通过配置文件进行绑定,然后将此Bean归还给容器

    1)配置文件内容

#数据源

spring.datasource.druid.write.url=jdbc:mysql://localhost:3306/jpa

spring.datasource.druid.write.username=root

spring.datasource.druid.write.password=1

spring.datasource.druid.write.driver-class-name=com.mysql.jdbc.Driver

 

spring.datasource.druid.read.url=jdbc:mysql://localhost:3306/jpa

spring.datasource.druid.read.username=root

spring.datasource.druid.read.password=1

spring.datasource.druid.read.driver-class-name=com.mysql.jdbc.Driver

    2)java代码

@Configuration

public class DruidDataSourceConfig {

    /**

     * DataSource 配置

     * @return

     */

    @ConfigurationProperties(prefix = "spring.datasource.druid.read")

    @Bean(name = "readDruidDataSource")

    public DataSource readDruidDataSource() {

        return new DruidDataSource();

    }

 

 

    /**

     * DataSource 配置

     * @return

     */

    @ConfigurationProperties(prefix = "spring.datasource.druid.write")

    @Bean(name = "writeDruidDataSource")

    @Primary

    public DataSource writeDruidDataSource() {

        return new DruidDataSource();

    }

}

  prefix 属性指定了配置属性,如spring.datasource.druid.write配置文件中spring.datasource.druid.write下有四个属性,然后会自动和方法下的对应的bean进行绑定。这里绑定的就是DataSource类型的对象这个绑定是隐式的,意味着配置文件编写的属性名和要绑定的对象的属性名要一致

 

  3.2@ConfigurationProperties作用于类

    1)配置文件

spring.datasource.url=jdbc:mysql://127.0.0.1:8888/test?useUnicode=false&autoReconnect=true&characterEncoding=utf-8

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.type=com.alibaba.druid.pool.DruidDataSource

    2)java代码

@ConfigurationProperties(prefix = "spring.datasource")

@Component

public class DatasourcePro {

 

    private String url;

 

    private String username;

 

    private String password;

 

    // 配置文件中是driver-class-name, 转驼峰命名便可以绑定成

    private String driverClassName;

 

    private String type;

 

    public String getUrl() {

        return url;

    }

    public void setUrl(String url) {

        this.url = url;

    }

    public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username;

    }

 

    public String getPassword() {

        return password;

    }

 

    public void setPassword(String password) {

        this.password = password;

    }

 

    public String getDriverClassName() {

        return driverClassName;

    }

 

    public void setDriverClassName(String driverClassName) {

        this.driverClassName = driverClassName;

    }

 

    public String getType() {

        return type;

    }

 

    public void setType(String type) {

        this.type = type;

    }

}

 

@Controller

@RequestMapping(value = "/config")

public class ConfigurationPropertiesController {

 

    @Autowired

    private DatasourcePro datasourcePro;

 

    @RequestMapping("/test")

    @ResponseBody

    public Map<String, Object> test(){

 

        Map<String, Object> map = new HashMap<>();

        map.put("url", datasourcePro.getUrl());

        map.put("userName", datasourcePro.getUsername());

        map.put("password", datasourcePro.getPassword());

        map.put("className", datasourcePro.getDriverClassName());

        map.put("type", datasourcePro.getType());

        return map;

    }

}

  @ConfigurationProperties @value 有着相同的功能,但是 @ConfigurationProperties的写法更为方便

  @ConfigurationProperties POJO类的命名比较严格,因为它必须和prefix的后缀名要一致, 不然值会绑定不上, 特殊的后缀名是“driver-class-name”这种带横杠的情况,POJO里面的命名规则是 下划线转驼峰就可以绑定成功,所以就是 “driverClassName”

通过上面的例子我们知道了,自己创建配置属性。它还有很多其他的用途。比如分页查询。我们可以自定义每页的数量作为一个配置属性来使用。

   3.3@ConfigurationProperties实例分页配置

      1)配置

mytxt:

    myconfig:

      page: 10

     size: asd

注意:配置的时候mytxt一定要和其它的配置竖向对齐,属性名和值之前要有空格

     2)代码

package tacos;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "mytxt.myconfig")

@Component

public class PagePro {

private Integer page;

 

private String size ;

 

public Integer getPage() {

return page;

}

 

public void setPage(Integer page) {

this.page = page;

}

 

public String getSize() {

return size;

}

 

public void setSize(String size) {

this.size = size;

}

}

@Controller

public class pageController {

    private PagePro pp;

    public pageController(PagePro pp) {

super();

this.pp = pp;

}

 
@RequestMapping("/config/pagetest")

    @ResponseBody

    public Integer test(){

        System.out.println(pp.getPage());

        System.out.println(pp.getSize());

        return pp.getPage();
    }
}

 4.申明配置属性元数据-即配置属性的注释

    在META-INF文件夹下创建一个文件additional-spring-cinfiguration-medadata.json 文件。在里面输入下面格式

{

"properties":[
  {
  "name":"mytxt.myconfig.page"

  "type":"java.lang.Integer"

  "description":"描述"

  }
]

}

  5.Profile配置

  它是一种条件化配置,在运行时,根据哪些Profile处于激活状态,相对于的使用配置属性。

  比如,在测试阶段,我么使用嵌入式数据库H2,日志采用debug。生产环境,数据库采用mysql,日志采用WARN

  那么怎么创建一个个的profile配置呢?

  5.1创建多个profile配置

    5.1.1第一种方法-创建多个文件

      例子:构建生产环境的配置

      1)新建一个yamlproperties文件,文件名要按照application-名字.yaml或者application-名字.properties。还是使用yaml文件比较好。这里我们创建一个application-sc.yaml文件

      2)里面配置生产环境的属性

spring:

  datasource:

    url: jdbc:mysql://localhost:3306/text?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC

    username: root

    password: 573875306

    driver-class-name: com.mysql.jdbc.Driver

logging:

  level:

    tacos:WARN

    -在一个application.yaml这一个文件中写

    多个profile主键用三个横线隔开,且使用spring.profiles属性来为profile命名。如下:

    三个横线上面有配置,这个配置没有使用spring.profiles属性来命名。这些属性就是通用属性,对每个profile都有效,当通用属性和profile的属性重复时,以profile为准。这里只配置了通用属性和一个名为scprofile,若还需要配置其它的,在下面加三条横线继续配置即可

logging:

  level:

    tacos:DEBUG

---

spring:

profiles:sc

 

  datasource:

    url: jdbc:mysql://localhost:3306/text?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC

    username: root

    password: 573875306

    driver-class-name: com.mysql.jdbc.Driver

logging:

  level:

    tacos:WARN

  5.2Profile激活

  上面讲了账目配置多个profile配置,却没有说怎么激活。

  在application.yaml中配置,指定默认激活的profile

spring:

  profiles:

    active: sc

 

  当需要变的时候: % export SPRING_PROFILES_ACTIVE_sc,sc指要使用的profile的名字

  如果是以jar文件运行的,在启动的时候可以使用命令行参数

  java -jar taco-cloud.jar --spring.profiles.active=sc

  补充说明:profile可以同时激活多个

  % export SPRING_PROFILES_ACTIVE_sc1,sc2,sc3

  java -jar taco-cloud.jar --spring.profiles.active=sc1,sc2,sc3

  spring:

    profiles:

      active: 

    -sc1

    -sc2

    -sc3

  5.3使用profile条件化的创建bean

  正常情况下,不管哪个profile被激活,JAVA配置类中声明的所有bean都会被创建。我们可以通过配置使某个bean在特定的profile被激活的时候才会被创建

  @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件

  使用@profile(“profile的名称”),多个用逗号隔开,还可以使用”!profile的名称,感叹号表示

package com.spring.config;

 

import java.beans.PropertyVetoException;

 

import javax.sql.DataSource;

 

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.EmbeddedValueResolverAware;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Profile;

import org.springframework.context.annotation.PropertySource;

import org.springframework.util.StringValueResolver;

 

import com.mchange.v2.c3p0.ComboPooledDataSource;

 

@Configuration

public class MainConfigOfProfile implements EmbeddedValueResolverAware{

 

@Value("${db.user}")

private String user;

 

private String driverClass;

 

@Profile("default")

@Bean("test")

public DataSource testDataSource(@Value("${db.password}")String password) throws PropertyVetoException {

ComboPooledDataSource dataSource = new ComboPooledDataSource();

dataSource.setUser(user);

dataSource.setPassword(password);

dataSource.setDriverClass(driverClass);

return dataSource;

}

}

原文地址:https://www.cnblogs.com/jthr/p/13647293.html