gpt4 book ai didi

java - 如何将@ConditionalOnProperty继承到任何静态内部@Configuration类?

转载 作者:行者123 更新时间:2023-11-30 05:32:18 24 4
gpt4 key购买 nike

如何创建一个包含静态内部配置类并继承@ConditionalOnProperty@Configuration类?

@Configuration
@ConditionalOnProperty(value = "my.property", havingValue = "true")
public class MyConfiguration {
@Configuration
static class SubConfig1 {
}

@Configuration
static class SubConfig2 {
}

....
}

在此示例中,我希望仅当父级的条件为 true 时才加载内部类,因此我不必为每个配置类重复条件。这可能吗?

最佳答案

您的理解是错误的,所有用 @Configuration 注释的嵌套类都会自动加载。 With nested @Configuration classes

When bootstrapping such an arrangement, only AppConfig need be registered against the application context. By virtue of being a nested @Configuration class, DatabaseConfig will be registered automatically. This avoids the need to use an @Import annotation when the relationship between AppConfig and DatabaseConfig is already implicitly clear.

因此,通过使用@ConditionalOnProperty,您只能避免在配置类中声明的bean,但无法避免带有@Configuration注释的嵌套类,除非由a指定情况

例如如果my.propertyfalse,下面的方法将避免将bean加载到ApplicationContext中p>

@Configuration
@ConditionalOnProperty(value = "my.property", havingValue = "true")
public class MyConfiguration {

@Bean
public SubConfig1 getSubConfig1() {
return new SubConfig1();
}

@Bean
public SubConfig2 getSubConfig2() {
return new SubConfig2();
}

static class SubConfig1 {
}


static class SubConfig2 {
}


}

关于java - 如何将@ConditionalOnProperty继承到任何静态内部@Configuration类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57274788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com