我想基于application.yml
. 我以为我首先排除了自动配置类,然后将其作为导入添加到我的配置类中,该配置类以该属性为条件:
MyApplication.class:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
ApplicationProperties.class
@Getter
@Setter
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapplication")
public class ApplicationProperties {
private boolean security;
}
应用程序.yml:
---
myapplication:
security: true
安全配置类:
@Configuration
@Import(SecurityAutoConfiguration.class)
@ConditionalOnProperty(prefix = "myapplication", name = "security", havingValue = "true")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
....
}
该类SecurityAutoConfiguration
永远不会被导入:
Exclusions:
-----------
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
那我怎样才能达到预期的结果呢?
更新
有趣的是,如果我KafkaAutoConfiguration.class
按照我的预期做同样的事情——所有的 Kafka bean 都会被加载和自动配置。
KafkaAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.kafka.core.KafkaTemplate' (OnClassCondition)
KafkaAutoConfiguration#kafkaAdmin matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.KafkaAdmin; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaConsumerFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.ConsumerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaProducerFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.ProducerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaProducerListener matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.support.ProducerListener; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaTemplate matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.KafkaTemplate; SearchStrategy: all) did not find any beans (OnBeanCondition)
Exclusions:
-----------
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration