search
HomeJavajavaTutorialExample tutorial on using Spring Boot Profiles

Example tutorial on using Spring Boot Profiles

Jun 17, 2017 pm 01:24 PM
bootprofilesspringExampleuse

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor