环保材料东莞网站建设,网站设计导航,中国工业设计十佳公司,网站做二级站在spring boot中有时候需要控制配置类是否生效,可以使用ConditionalOnProperty注解来控制Configuration是否生效 步骤一#xff1a;配置类代码 Configuration
ConditionalOnProperty(prefix application, value enable, havingValue trueConditionalOnProperty注解来控制Configuration是否生效 步骤一配置类代码 Configuration
ConditionalOnProperty(prefix application, value enable, havingValue true)
public class TestConfig {PostConstructpublic void test() {System.out.println(初始化配置);}
} 步骤二配置文件代码 application.enabletrue 步骤三输出结果
步骤四总结
当不在配置配置或配置其值是false时则此配置类不会生效。在系统启动时也就不会打印这个日志信息。只有设置为true时才会打印。
ConditionalOnProperty是通过havingValue的值与配置文件中的值对比返回为true则配置类生效反之失效。其中prefix是配置的前缀name是属性名。