Home  >  Article  >  Java  >  Example tutorial on using Spring Boot Profiles

Example tutorial on using Spring Boot Profiles

零下一度
零下一度Original
2017-06-17 13:24:441530browse

This article mainly introduces the configuration and use of Spring Boot Profiles in detail, which has certain reference value. Those who are interested can learn more

Introduction

Spring Profiles provides a set of ways to isolate application configurations. Different profiles provide different combinations of configurations. In different environments, applications can adapt by choosing to activate certain profiles at startup. Runtime environment to achieve the same set of program code can be used in different environments.

Environment

  1. ##JDK 8

  2. Maven 3

  3. IntelliJ IDEA 2016

  4. ##Spring Boot 1.5.2.RELEASE
  5. ##@Profiles

You can use the @Profiles annotation in any class annotated with @Component (@Service, @Repository) or @Configuration annotation:


public interface PaymentService {
  String createPaymentQrcode();
}


@Service
@Profile("alipay")
public class AlipayService implements PaymentService {
  @Override
  public String createPaymentQrcode() {
    return "支付宝支付二维码";
  }
}


@Service
@Profile({"default", "wechatpay"})
public class WechatpayService implements PaymentService {
  @Override
  public String createPaymentQrcode() {
    return "微信支付二维码";
  }
}

In Spring Boot, the default profile is default, therefore,
PaymentService.createPaymentQrcode() ->

WeChat payment 2D code.

You can activate a specific profile through
spring.profiles.active

:


##

java -jar -Dspring.profiles.active='alipay' xxx.jar

PaymentService.createPaymentQrcode() ->
Alipay payment QR code.

Multiple environment configuration

In Spring Boot, multi-environment configuration file
can use application-{profile}. {properties|yml}.


@Component
@ConfigurationProperties("jdbc")
public class JdbcProperties {
  private String username;
  private String password;
  // getters and setters
}

Development environment

application-dev.properties
Configuration:


jdbc.username=root
jdbc.password=123654

Production environment

application-prod.properties
Configuration:

##

jdbc.username=produser
jdbc.password=16888888

or:


Development environment

application-dev.yml

Configuration:

jdbc:
 username: root
 password: 123654

Production environment

application-prod.yml

Configuration:

jdbc:
 username: produser
 password: 16888888

Or:


Use only application.yml and create multi-profile configurations in this file by --- separator:


app:
 version: 1.0.0
spring:
 profiles:
  active: "dev"
---
spring:
 profiles: dev
jdbc:
 username: root
 password: 123654
---
spring:
 profiles: prod
jdbc:
 username: produser
 password: 16888888

Command line startup:

java -jar -Dspring.profiles.active=prod xxxx.jar

The above is the detailed content of Example tutorial on using Spring Boot Profiles. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn