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
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
Development environment
Configuration:
jdbc: username: root password: 123654
Configuration:
jdbc: username: produser password: 16888888
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
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!