{"id":2224,"date":"2022-02-24T11:22:08","date_gmt":"2022-02-24T03:22:08","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2224"},"modified":"2022-02-24T11:22:27","modified_gmt":"2022-02-24T03:22:27","slug":"conditionalonmissingclass%e7%ad%89","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2224","title":{"rendered":"@ConditionalOnMissingClass\u7b49"},"content":{"rendered":"\n<p>\u5176\u5b83\u6ce8\u91ca\u53ca\u603b\u7ed3<\/p>\n\n\n\n<ul><li>@ConditionalOnBean \/\/ \u5f53\u7ed9\u5b9a\u7684\u5728bean\u5b58\u5728\u65f6,\u5219\u5b9e\u4f8b\u5316\u5f53\u524dBean<\/li><li>@ConditionalOnMissingBean \/\/ \u5f53\u7ed9\u5b9a\u7684\u5728bean\u4e0d\u5b58\u5728\u65f6,\u5219\u5b9e\u4f8b\u5316\u5f53\u524dBean<\/li><li>@ConditionalOnClass \/\/ \u5f53\u7ed9\u5b9a\u7684\u7c7b\u540d\u5728\u7c7b\u8def\u5f84\u4e0a\u5b58\u5728\uff0c\u5219\u5b9e\u4f8b\u5316\u5f53\u524dBean<\/li><li>@ConditionalOnMissingClass \/\/ \u5f53\u7ed9\u5b9a\u7684\u7c7b\u540d\u5728\u7c7b\u8def\u5f84\u4e0a\u4e0d\u5b58\u5728\uff0c\u5219\u5b9e\u4f8b\u5316\u5f53\u524dBean<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u770b\u4e0b\u4ee5\u4e0b\u7684\u4f8b\u5b50\uff0c\u53c2\u8003\uff1a<a href=\"https:\/\/www.jb51.net\/article\/193224.htm\">springboot @ConditionalOnMissingBean\u6ce8\u89e3\u7684\u4f5c\u7528\u8be6\u89e3_java_\u811a\u672c\u4e4b\u5bb6 (jb51.net)<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@ConditionalOnMissingBean\uff0c\u5b83\u662f\u4fee\u9970bean\u7684\u4e00\u4e2a\u6ce8\u89e3\uff0c\u4e3b\u8981\u5b9e\u73b0\u7684\u662f\uff0c\u5f53\u4f60\u7684bean\u88ab\u6ce8\u518c\u4e4b\u540e\uff0c\u5982\u679c\u800c\u6ce8\u518c\u76f8\u540c\u7c7b\u578b\u7684bean\uff0c\u5c31\u4e0d\u4f1a\u6210\u529f\uff0c\u5b83\u4f1a\u4fdd\u8bc1\u4f60\u7684bean\u53ea\u6709\u4e00\u4e2a\uff0c\u5373\u4f60\u7684\u5b9e\u4f8b\u53ea\u6709\u4e00\u4e2a\uff0c\u5f53\u4f60\u6ce8\u518c\u591a\u4e2a\u76f8\u540c\u7684bean\u65f6\uff0c\u4f1a\u51fa\u73b0\u5f02\u5e38\uff0c\u4ee5\u6b64\u6765\u544a\u8bc9\u5f00\u53d1\u4eba\u5458\u3002\n\n@Component\npublic class AutoConfig {\n  @Bean\n  public AConfig aConfig() {\n    return new AConfig(\"lind\");\n  }\n \n  @Bean\n  @ConditionalOnMissingBean(AMapper.class)\n  public AMapper aMapper1(AConfig aConfig) {\n    return new AMapperImpl1(aConfig);\n  }\n \n  @Bean\n  public AMapper aMapper2(AConfig aConfig) {\n    return new AMapperImpl2(aConfig);\n  }\n}\n\n\n\u56e0\u4e3a\u5728aMapper1\u4e0a\u9762\u6807\u8bc6\u4e86AMapper\u7c7b\u578b\u7684bean\u53ea\u80fd\u6709\u4e00\u4e2a\u5b9e\u73b0 @ConditionalOnMissingBean(AMapper.class)\uff0c\u6240\u4ee5\u5728\u8fdb\u884caMapper2\u6ce8\u518c\u65f6\uff0c\u7cfb\u7edf\u4f1a\u51fa\u73b0\u5f02\u5e38\n\u5f53\u6211\u4eec\u628a @ConditionalOnMissingBean(AMapper.class) \u53bb\u6389\u4e4b\u540e\uff0c\u4f60\u7684bean\u53ef\u4ee5\u6ce8\u518c\u591a\u6b21\uff0c\u8fd9\u65f6\u9700\u8981\u7528\u7684@Primary\u6765\u786e\u5b9a\u4f60\u8981\u54ea\u4e2a\u5b9e\u73b0\uff1b\u4e00\u822c\u6765\u8bf4\uff0c\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u7684\u914d\u7f6e\u7c7b\uff0c\u6211\u4eec\u5e94\u8be5\u52a0\u4e0a@ConditionalOnMissingBean\u6ce8\u89e3\uff0c\u4ee5\u907f\u514d\u591a\u4e2a\u914d\u7f6e\u540c\u65f6\u6ce8\u5165\u7684\u98ce\u9669\u3002\n\n\n@Primary\u6807\u8bc6\u54ea\u4e2a\u662f\u9ed8\u8ba4\u7684bean\n\n@Bean\npublic AMapper aMapper1(AConfig aConfig) {\n  return new AMapperImpl1(aConfig);\n}\n \n@Bean\n@Primary\npublic AMapper aMapper2(AConfig aConfig) {\n  return new AMapperImpl2(aConfig);\n}\n\n\n\n@ConditionalOnProperty\n\u901a\u8fc7\u5176\u4e09\u4e2a\u5c5e\u6027prefix,name\u4ee5\u53cahavingValue\u6765\u5b9e\u73b0\u7684\uff0c\u5176\u4e2dprefix\u8868\u793a\u914d\u7f6e\u6587\u4ef6\u91cc\u8282\u70b9\u524d\u7f00\uff0cname\u7528\u6765\u4eceapplication.properties\u4e2d\u8bfb\u53d6\u67d0\u4e2a\u5c5e\u6027\u503c\uff0chavingValue\u8868\u793a\u76ee\u6807\u503c\u3002\n\n\u5982\u679c\u8be5\u503c\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56defalse;\n\u5982\u679c\u503c\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u503c\u4e0ehavingValue\u6307\u5b9a\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u4e00\u6837\u5219\u8fd4\u56detrue;\u5426\u5219\u8fd4\u56defalse\u3002\n\u8fd4\u56de\u503c\u4e3afalse\uff0c\u5219\u8be5configuration\u4e0d\u751f\u6548\uff1b\u4e3atrue\u5219\u751f\u6548\u3002\n\u4e0b\u9762\u4ee3\u7801\u6f14\u793a\u4e3a\u914d\u7f6e\u6587\u4ef6lind.redis.enable\u4e3atrue\u65f6\u624d\u4f1a\u6ce8\u518cRedisFactory\u8fd9\u4e2abean\n\n@Configuration\n@ConditionalOnProperty(prefix=\"lind.redis\",name = \"enable\", havingValue = \"true\")\npublic class RedisConfig {\n  @Bean\n  public RedisMap redisMap(){\n    return new RedisMapImpl();\n  }\n}\n\n<\/pre>\n\n\n\n<p>\u518d\u770b\u4e0b@Conditional\u7684\u7528\u6cd5\uff0c\u53c2\u8003\uff1a<a href=\"https:\/\/www.1024sky.cn\/blog\/article\/408\">spring\u6ce8\u89e3@ConditionalOnMissingClass\u8be6\u7ec6\u4f7f\u7528\u8bf4\u660e (1024sky.cn)<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Conditional\uff0c\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u521b\u5efa\u4e00\u4e2aBean\uff0cSpringBoot\u5c31\u662f\u5229\u7528\u8fd9\u4e2a\u7279\u6027\u8fdb\u884c\u81ea\u52a8\u914d\u7f6e\u7684\u3002\n\u4f8b\u5b50:\n\u9996\u5148\uff0c\u4e24\u4e2aCondition\uff0c\u5224\u65ad\u5f53\u524d\u7cfb\u7edf\u662f\u5426\u662fWindows\u6216\u8005Linux\uff08True False\uff09\n\npublic class LinuxCondition implements Condition {\n\n    @Override\n    public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {\n        return arg0.getEnvironment().getProperty(\"os.name\").contains(\"Linux\");\n    }\n\n}\npublic class WindowsCondition implements Condition {\n\n    @Override\n    public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {\n        return arg0.getEnvironment().getProperty(\"os.name\").contains(\"Windows\");\n    }\n\n}\n\n\n\u7136\u540e\uff0c2\u4e2aListService\u5b9e\u73b0\u7c7b\uff0c\u8868\u660e\u4e0d\u540c\u7cfb\u7edf\u4e0b\u7684ListService\u5b9e\u73b0\u3002\n\npublic interface ListService {\n    public String showListCmd();\n}\npublic class LinuxListService implements ListService{\n    @Override\n    public String showListCmd() {\n        return \"ls\";\n    }    \n}\npublic class WindowsListService implements ListService{\n    @Override\n    public String showListCmd() {\n        return \"dir\";\n    }    \n}\n\n\n\u7136\u540eConditionConfig\u4f7f\u7528\u4e86Java\u914d\u7f6e\u4e0e@Conditional\u6ce8\u89e3\uff0c\u6839\u636eLinuxCondition\uff0c\u6216\u8005WindowsCondition\u4f5c\u4e3a\u5224\u65ad\u6761\u4ef6\n\n@Configuration\npublic class ConditionConfig {\n\n    @Bean\n    @Conditional(WindowsCondition.class)\n    public ListService windowsListService() {\n        return new WindowsListService();\n    }\n\n    @Bean\n    @Conditional(LinuxCondition.class)\n    public ListService linuxListService() {\n        return new LinuxListService();\n    }\n\n}\n\u6700\u540e\uff0cApp.java \u6d4b\u8bd5\u6210\u529f\u3002\n\npublic class App {\n    public static void main(String[] args) {\n\n        ApplicationContext context = new AnnotationConfigApplicationContext(\n                ConditionConfig.class);\n\n        ListService ls = context.getBean(ListService.class);\n        System.out.println(context.getEnvironment().getProperty(\"os.name\")\n                + \"\u7cfb\u7edf\u4e0b\u7684\u5217\u8868\u547d\u4ee4\u4e3a:\" + ls.showListCmd());\n    }\n}\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5176\u5b83\u6ce8\u91ca\u53ca\u603b\u7ed3 @ConditionalOnBean \/\/ \u5f53\u7ed9\u5b9a\u7684\u5728bean\u5b58\u5728\u65f6,\u5219\u5b9e\u4f8b\u5316\u5f53\u524dBean @ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[54],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2224"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2224"}],"version-history":[{"count":17,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2224\/revisions"}],"predecessor-version":[{"id":2241,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2224\/revisions\/2241"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}