{"id":8594,"date":"2022-11-18T12:19:35","date_gmt":"2022-11-18T04:19:35","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8594"},"modified":"2022-11-19T10:04:25","modified_gmt":"2022-11-19T02:04:25","slug":"springboot-%e6%b3%a8%e5%85%a5-%e4%b8%8d%e5%90%8c%e5%8c%85%e4%b8%ad%e7%b1%bb%e5%90%8d%e7%9b%b8%e5%90%8c%e5%86%b2%e7%aa%81%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8594","title":{"rendered":"Springboot  \u6ce8\u5165 \u4e0d\u540c\u5305\u4e2d\u7c7b\u540d\u76f8\u540c\u51b2\u7a81\u95ee\u9898\u89e3\u51b3"},"content":{"rendered":"\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>springboot \u5728\u542f\u52a8\u65f6\u5019\uff0c\u5e38\u542f\u52a8\u4e0d\u8d77\u6765\uff0c\u68c0\u67e5\u53d1\u73b0\u662f\u4e0d\u540c\u5305\u4e0b\u9762\u6709\u540c\u540d\u7684service\u548cserviceImpl\uff0c\u6309\u7406\u8bf4\u4e0d\u540c\u5305\u4e0b\u662f\u53ef\u4ee5\u6709\u540c\u540d\u7684\u7c7b\u5b58\u5728\u7684\uff0c\u4f46\u662f\u542f\u52a8\u5c31\u662f\u542f\u52a8\u4e0d\u4e86\uff0c\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'roleServiceImpl' for bean class [com.example.service.RoleServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [com.example.roleService.RoleServiceImpl]\r\n<\/pre>\n\n\n\n<p>\u610f\u601d\u662f\uff1a\u4ee5Bean\u540d\u5b57\u2018roleServiceImpl\u2019\u6ce8\u89e3\u7684\u7c7b[com.example.service.RoleServiceImpl]\u4e0e\u5b58\u5728\u7684\u4e0d\u76f8\u5bb9\u7684\u540c\u540d\u7c7b[com.example.roleService.RoleServiceImpl]\u76f8\u51b2\u7a81\u3002<\/p>\n\n\n\n<p>\u539f\u6765\u662f\u5728\u8fd9\u4e24\u4e2a\u5b9e\u73b0\u7c7b\u4e0a\u9762\u90fd\u53ea\u7528\u4e86@service\u8fd9\u4e2a\u6ce8\u89e3\uff0c\u6839\u636e\u6620\u5c04\u89c4\u5219\uff0c\u8fd9\u4e24\u4e2aservice\u90fd\u6620\u5c04\u4e3a\u4e86roleServiceImpl,\u53d1\u751f\u4e86\u51b2\u7a81\u3002<\/p>\n\n\n\n<p>\u51fa\u73b0\u51b2\u7a81\u539f\u56e0\uff1aspring\u63d0\u4f9b\u4e24\u79cdbeanName\u751f\u6210\u7b56\u7565\uff0c\u57fa\u4e8e\u6ce8\u89e3\u7684sprong-boot\u9ed8\u8ba4\u4f7f\u7528\u7684\u662fAnnotationBeanNameGenerator\uff0c\u5b83\u751f\u6210beanName\u7684\u7b56\u7565\u5c31\u662f\uff0c\u53d6\u5f53\u524d\u7c7b\u540d\uff08\u4e0d\u662f\u5168\u9650\u5b9a\u7c7b\u540d\uff09\u4f5c\u4e3abeanName\u3002\u7531\u6b64\uff0c\u5982\u679c\u51fa\u73b0\u4e0d\u540c\u5305\u7ed3\u6784\u4e0b\u540c\u6837\u7684\u7c7b\u540d\u79f0\uff0c\u80af\u5b9a\u4f1a\u51fa\u73b0\u51b2\u7a81\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\u4e00<\/strong>\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>1.\u91cd\u65b0\u5b9a\u4e49beanName\u751f\u6210\u7b56\u7565\uff0c\u7ee7\u627fAnnotationBeanNameGenerator\uff0c\u91cd\u5199generateBeanName\uff0c\u5982\u4e0b\u6240\u793a\uff1a<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class UniqueNameGenerator extends AnnotationBeanNameGenerator {\n \n    @Override\n    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {\n \n        \/\/\u5982\u679c\u6709\u8bbe\u7f6e\u4e86value\uff0c\u5219\u7528value\uff0c\u5982\u679c\u6ca1\u6709\u5219\u662f\u7528\u5168\u7c7b\u540d\n        if (definition instanceof AnnotatedBeanDefinition) {\n            String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);\n            if (StringUtils.hasText(beanName)) {\n                \/\/ Explicit bean name found.\n                return beanName;\n            }else{\n                \/\/\u5168\u9650\u5b9a\u7c7b\u540d\n                beanName = definition.getBeanClassName();\n                return beanName;\n            }\n        }\n \n        \/\/ \u4f7f\u7528\u9ed8\u8ba4\u7c7b\u540d\n        return buildDefaultBeanName(definition, registry);\n    }\n}<\/pre>\n\n\n\n<p>2.\u5728\u542f\u52a8\u7c7b\u4e0a\u52a0\u6ce8\u89e3@ComponentScan(nameGenerator = UniqueNameGenerator.class)\u4f7f\u521a\u624d\u6211\u4eec\u81ea\u5b9a\u4e49\u7684BeanName\u751f\u6210\u7b56\u7565\u751f\u6548\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\n@ComponentScan(nameGenerator = UniqueNameGenerator.class)\npublic class TestApplication {\n  public static void main(String[] args) {\n    SpringApplication.run(TestApplication.class, args);\n  }\n}<\/pre>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\u4e8c\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5c06\u5176\u4e2d\u4e00\u4e2a\u6ce8\u89e3\u53d8\u66f4\u4e3a\u4e00\u4e2aname\u4e3a\u975eroleServiceImpl\u7684\u6ce8\u89e3@service(name=&#8221;aaaa&#8221;)\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\u4e09\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5c06\u5176\u4e2d\u4e00\u4e2a\u5b9e\u73b0\u7c7b\u6539\u4e3a\u4e0d\u540c\u7684\u540d\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\uff1a springboot \u5728\u542f\u52a8\u65f6\u5019\uff0c\u5e38\u542f\u52a8\u4e0d\u8d77\u6765\uff0c\u68c0\u67e5\u53d1\u73b0\u662f\u4e0d\u540c\u5305\u4e0b\u9762\u6709\u540c\u540d\u7684service\u548cservi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8594"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8594"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8594\/revisions"}],"predecessor-version":[{"id":8618,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8594\/revisions\/8618"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}