springboot load multiple profiles

  The default configuration file

  When we create a springboot project, the system creates a default application.properties for us in src / main / Java / resources directory.

  How springboot load multiple profiles simultaneously

  springboot application.properties default is to load the default configuration file in src / main / resources folder

  Format application- {profile} .properties, wherein {profile} identification corresponding to your environment

  Add spring.profiles.active = dev, database in the application.properties

  # Load multiple configuration files, the system loads the application.properties application-database.properties application-dev.properties three profiles

  spring.profiles.active = dev,database

  The system is loaded application.properties application-database.properties under src / main / resources directory application-dev.properties three profiles

  The property profile is assigned to the entity classes

  application-dev.properties file contents

  dev.name = liumei

  entity class java

  package com.hb.config;

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

  import org.springframework.stereotype.Component;

  @Component

  @ConfigurationProperties(prefix="dev")

  public class DevConfig {

  // value of application-dev.properties in dev.name

  private String name;

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this.name = name;

  }

  }

  @ConfigurationProperties (prefix = "dev") means that the load values ​​of all the configuration information dev.name; name attribute get and set methods have

  Custom Profile

  Described above is that we regard the configuration file is written in application.yml. Sometimes we do not want to configure the application configuration files are written, then we need to customize the configuration files, such test.properties:

  com.forezp.name=forezp

  com.forezp.age=12

  How this information is given to a configuration file javabean it?

  @Configuration

  @PropertySource(value = "classpath:test.properties")

  @ConfigurationProperties(prefix = "com.forezp")

  public class User {

  private String name;

  private int age;

  public String getName() {

  return name;

  } Wuxi good ××× hospital http://www.zzch120.com/

  public void setName(String name) {

  this.name = name;

  }

  public int getAge() {

  return age;

  }

  public void setAge(int age) {

  this.age = age;

  }

  }

  In the latest version of springboot, you need to add three comments.

  @Configuration

  @PropertySource(value = “classpath:test.properties”)

  @ConfigurationProperties (prefix = "com.forezp"); requires version 1.4


Guess you like

Origin blog.51cto.com/14335413/2425151
Recommended