Spring Boot yml config可以覆盖其他模块的配置吗?

时间:2019-08-21 15:27:10

标签: spring spring-boot

我想从其他模块覆盖yaml config的一个字段。但是失败了。假设有两个项目:主模块和支持模块。 在支持模块中。代码如下:

KubernetesPodOperator(
    task_id=task_id,
    namespace='default',
    image=image,
    arguments=arguments,
    name=task_id.replace('_', '-'),
    affinity={
        'nodeAffinity': {
            'requiredDuringSchedulingIgnoredDuringExecution': {
                'nodeSelectorTerms': [{
                    'matchExpressions': [{
                        'key': 'cloud.google.com/gke-nodepool',
                        'operator': 'In',
                        'values': [
                            '<name_of_your_node_pool>',
                        ]
                    }]
                }]
            }
        }
    },
    is_delete_operator_pod=True,
    hostnetwork=False,
)

和名为“ application-support-dev.yml”的Yaml,内容为:

@Component
@Data
public class SupportPart {
    @Value("${support.val0}")
    private String val0;

    private String val1;

    @Value("${support.val1}")
    public void setVal1(String val1) {
        this.val1 = val1;
    }
}

在主模块中,主要代码为:

support.val0: "support.val0 is from application-support-dev"
support.val1: "support.val1 is from application-support-dev"

有一个主条目看起来像:

@Component
@Data
public class MainPart {
    @Value("${main.val0}")
    private String val0;
    @Value("${main.val1}")
    private String val1;
    @Value("${main.val2}")
    private String val2;

    @Value("${main.val3}")
    private String val3;

    @Autowired
    private SupportPart part;
}

而yaml文件是: application.yml:

@SpringBootApplication
public class SpringBootConsoleApplication
    implements CommandLineRunner {

    @Autowired
    private MainPart mainPart;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootConsoleApplication.class, args);
    }

    @Override
    public void run(String... args) {
        System.out.println(mainPart.getVal0());
        System.out.println(mainPart.getVal1());
        System.out.println(mainPart.getVal2());
        System.out.println(mainPart.getVal3());
        System.out.println(mainPart.getPart().getVal0());
        System.out.println(mainPart.getPart().getVal1());
    }
}

application-dev.yml

main:
  val3: "main.val3 from application"

application-main.yml:

spring.profiles.include: main,support-dev

main:
  val0: "main.val0 from application-dev"
  val1: "main.val1 from application-dev"

support:
  val1: "support.val1 is from application-dev"

运行时,我使用-Dspring.profiles.active = dev,因此开发人员配置文件将处于活动状态,并将包括application-main.yml,application-support-dev.yml。

因为support.val1是在application-dev.yml和application-support-dev.yml中定义的(这来自另一个项目)。因此,我认为对于support.val1,application-dev.yml将覆盖application-support-dev.yml。我以为结果应该是:

main:
  val0: "main.val0 from application-main"
  val2: "main.val2 from application-main"

但是结果实际上是:

main.val0 from application-main
main.val1 from application-dev
main.val2 from application-main
main.val3 from application
support.val0 is from application-support-dev
support.val1 is from application-dev

有人知道为什么吗?还是Spring Boot中的配置覆盖机制?

0 个答案:

没有答案