PS 使用 Spring Boot 3.1.2 进行测试
1.使用@ConditionalOnProperty
@ConditionalOnProperty
仅当特定属性存在或具有特定值时,注释才会创建 Bean 。
在此示例中,仅当或文件中的CommandLineRunner
属性db.init.enabled
设置为 true时,才会执行。application.properties``application.yml
数据库初始化器.java
package com.yuanmomo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(
name = "db.init.enabled",
havingValue = "true",
matchIfMissing = false
)
public class DatabaseInitializer implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("This runs when 'db.init.enabled' property is tru