zoukankan      html  css  js  c++  java
  • @Configuration,@ConfigurationProperties,@EnableConfigurationProperties

    @Configuration

    @ConfigurationProperties

    不能单独使用必须使用带有可以实例化bena的注解比如:@Component或者组合了@Component的注解

    @ConfigurationProperties配合@EnableConfigurationProperties使用

    可以实现@ConfigurationProperties所注解类是否实例化,由@EnableConfigurationProperties所注解的类决定。

    @Data
    @ConfigurationProperties(prefix="my.test")
    public class MyServerConfiguration {
        private String name;
    }
    
    
    @Component
    @EnableConfigurationProperties(MyServerConfiguration.class)
    public class MyServerConfigutation2 {
    
    }
    

    MyServerConfigutation2这个类如果没有实例化MyServerConfiguration这个配置类也不会实例化!

    联合使用

    @Configuration,@ConfigurationProperties配合@ConditionalOnProperty使用,实现效果:

    @Configuration
    @ConditionalOnProperty(prefix = "my.test", value = "enabled", havingValue = "true")
    @EnableConfigurationProperties(MyServerConfiguration.class)
    public class MyServerConfigutation2 {
    
    }
    
    @Data
    @ConfigurationProperties(prefix="my.test")
    public class MyServerConfiguration {
        private String name;
    }
    

    只有配置文件中指定了:my.test.enabled=true才会实例化MyServerConfigutation2 这个类,也就才会实例化MyServerConfiguration ;

  • 相关阅读:
    家庭记账本(七+每周总结)
    家庭记账本(六)
    家庭记账本(五)
    家庭记账本(四)
    家庭记账本(三)
    家庭记账本(二)
    家庭记账本(一)
    2021.2.14(每周总结)
    2021.2.13
    文件上传时报错in a frame because it set 'X-Frame-Options' to 'deny'.
  • 原文地址:https://www.cnblogs.com/wangsen/p/11655612.html
Copyright © 2011-2022 走看看