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
- ##JDK 8
- Maven 3
- IntelliJ IDEA 2016 ##Spring Boot 1.5.2.RELEASE
- ##@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() ->
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=123654Production environment
application-prod.properties
Configuration:
##
jdbc.username=produser jdbc.password=16888888
or:
Development environment
Configuration:
jdbc: username: root password: 123654
Production environmentapplication-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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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
The most popular open source editor