{"id":431,"date":"2024-12-21T23:43:36","date_gmt":"2024-12-21T15:43:36","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=431"},"modified":"2024-12-21T23:43:36","modified_gmt":"2024-12-21T15:43:36","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0-spring-%e4%b8%ad%e7%9a%84-nosuchbeandefinitionexception-%e9%94%99%e8%af%af%ef%bc%9f%e8%af%a5%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/21\/%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0-spring-%e4%b8%ad%e7%9a%84-nosuchbeandefinitionexception-%e9%94%99%e8%af%af%ef%bc%9f%e8%af%a5%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 Spring \u4e2d\u7684\u00a0NoSuchBeanDefinitionException\u00a0\u9519\u8bef\uff1f\u8be5\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Spring \u4e2d\uff0c<code>NoSuchBeanDefinitionException<\/code> \u9519\u8bef\u901a\u5e38\u8868\u793a\u5bb9\u5668\u4e2d\u627e\u4e0d\u5230\u6240\u8bf7\u6c42\u7684 Bean\u3002\u8fd9\u4e2a\u5f02\u5e38\u53d1\u751f\u7684\u539f\u56e0\u53ef\u80fd\u662f\u591a\u79cd\u591a\u6837\u7684\uff0c\u5305\u62ec\u914d\u7f6e\u9519\u8bef\u3001Bean \u7684\u6ce8\u518c\u95ee\u9898\u7b49\u3002\u4e0b\u9762\u8be6\u7ec6\u5206\u6790\u51fa\u73b0 <code>NoSuchBeanDefinitionException<\/code> \u9519\u8bef\u7684\u5e38\u89c1\u539f\u56e0\u53ca\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001<code>NoSuchBeanDefinitionException<\/code> \u9519\u8bef\u7684\u5e38\u89c1\u539f\u56e0<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>Bean \u672a\u6b63\u786e\u6ce8\u518c<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1aSpring \u5bb9\u5668\u627e\u4e0d\u5230\u6240\u8bf7\u6c42\u7684 Bean\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u8be5 Bean \u6ca1\u6709\u88ab\u6b63\u786e\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<ul><li>\u786e\u4fdd\u4f7f\u7528 <code>@Component<\/code>\u3001<code>@Service<\/code>\u3001<code>@Repository<\/code>\u3001<code>@Controller<\/code> \u7b49\u6ce8\u89e3\u6807\u6ce8\u4e86\u76f8\u5e94\u7684\u7c7b\uff0c\u6216\u8005\u901a\u8fc7 <code>@Bean<\/code> \u6ce8\u89e3\u6ce8\u518c\u5230\u914d\u7f6e\u7c7b\u4e2d\u3002<\/li><li>\u68c0\u67e5 <code>@ComponentScan<\/code> \u662f\u5426\u8986\u76d6\u4e86\u6b63\u786e\u7684\u5305\u8def\u5f84\uff0c\u786e\u4fdd Spring \u5bb9\u5668\u626b\u63cf\u5230\u76f8\u5173\u7684 Bean \u7c7b\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li><\/ul><code>@Service public class MyService { \/\/ \u670d\u52a1\u903b\u8f91 } @Configuration @ComponentScan(basePackages = \"com.example\") \/\/ \u786e\u4fdd\u626b\u63cf\u5230 MyService public class AppConfig { }<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>Bean \u7684\u540d\u79f0\u4e0d\u5339\u914d<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1aSpring \u5bb9\u5668\u9ed8\u8ba4\u4f1a\u4f7f\u7528\u7c7b\u540d\u7684\u5c0f\u5199\u7248\u672c\u4f5c\u4e3a Bean \u7684\u540d\u79f0\u3002\u5982\u679c\u4f60\u8bf7\u6c42\u7684 Bean \u540d\u79f0\u4e0e\u5b9e\u9645 Bean \u7684\u540d\u79f0\u4e0d\u5339\u914d\uff0c\u5c31\u4f1a\u629b\u51fa\u8be5\u5f02\u5e38\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<ul><li>\u786e\u4fdd\u4f60\u4f7f\u7528 <code>@Qualifier<\/code> \u6216 <code>@Autowired<\/code> \u65f6\uff0c\u5f15\u7528\u4e86\u6b63\u786e\u7684 Bean \u540d\u79f0\u3002<\/li><li>\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49 Bean \u540d\u79f0\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>@Bean<\/code> \u6216 <code>@Component<\/code> \u6ce8\u89e3\u7684 <code>value<\/code> \u5c5e\u6027\u6307\u5b9a Bean \u540d\u79f0\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li><\/ul><code>@Service(\"myService\") public class MyService { \/\/ \u670d\u52a1\u903b\u8f91 } @Autowired @Qualifier(\"myService\") \/\/ \u786e\u4fdd\u540d\u79f0\u4e00\u81f4 private MyService myService;<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>\u4f9d\u8d56\u6ce8\u5165\uff08@Autowired\uff09\u4e0d\u6b63\u786e<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1a\u5f53\u4f7f\u7528 <code>@Autowired<\/code> \u6ce8\u89e3\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684 Bean\uff0cSpring \u5c31\u4f1a\u629b\u51fa <code>NoSuchBeanDefinitionException<\/code>\u3002\u8fd9\u901a\u5e38\u53d1\u751f\u5728\u81ea\u52a8\u88c5\u914d\u7684 Bean \u7c7b\u578b\u6216\u540d\u79f0\u4e0e Spring \u5bb9\u5668\u4e2d\u7684\u5b9e\u9645 Bean \u4e0d\u5339\u914d\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<ul><li>\u786e\u4fdd\u81ea\u52a8\u88c5\u914d\u7684 Bean \u5b58\u5728\u4e14\u7c7b\u578b\u5339\u914d\u3002<\/li><li>\u4f7f\u7528 <code>@Qualifier<\/code> \u6216\u660e\u786e\u6307\u5b9a Bean \u540d\u79f0\u6765\u89e3\u51b3\u591a\u4e2a\u76f8\u540c\u7c7b\u578b\u7684 Bean \u6ce8\u5165\u95ee\u9898\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li><\/ul><code>@Autowired private MyService myService; \/\/ \u786e\u4fdd Spring \u4e2d\u6709 MyService \u7c7b\u578b\u7684 Bean \/\/ \u5982\u679c\u6709\u591a\u4e2a MyService \u7c7b\u578b\u7684 Bean\uff0c\u53ef\u4ee5\u4f7f\u7528 @Qualifier @Autowired @Qualifier(\"myService\") private MyService myService;<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4. <strong>Bean \u914d\u7f6e\u6587\u4ef6\u4e22\u5931\u6216\u914d\u7f6e\u9519\u8bef<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1aSpring \u7684\u914d\u7f6e\u6587\u4ef6\uff08\u5982 XML \u914d\u7f6e\u6587\u4ef6\uff09\u53ef\u80fd\u4e22\u5931\uff0c\u6216\u914d\u7f6e\u7684 Bean \u672a\u88ab\u6b63\u786e\u52a0\u8f7d\uff0c\u5bfc\u81f4\u5bb9\u5668\u4e2d\u627e\u4e0d\u5230\u8be5 Bean\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<ul><li>\u68c0\u67e5\u914d\u7f6e\u6587\u4ef6\u662f\u5426\u6b63\u786e\uff0c\u786e\u4fdd\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\u548c\u5185\u5bb9\u65e0\u8bef\u3002<\/li><li>\u5728 XML \u914d\u7f6e\u4e2d\uff0c\u4f7f\u7528 <code>context:component-scan<\/code> \u914d\u7f6e\u626b\u63cf\u8def\u5f84\uff0c\u6216\u8005\u786e\u4fdd <code>@Configuration<\/code> \u914d\u7f6e\u7c7b\u88ab\u6b63\u786e\u52a0\u8f7d\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li><\/ul><code>&lt;context:component-scan base-package=\"com.example\" \/><\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5. <strong>Profile \u4e0d\u5339\u914d<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1a\u5728\u4f7f\u7528 Spring Profiles \u65f6\uff0c\u53ef\u80fd\u4f1a\u5b58\u5728\u67d0\u4e9b Bean \u53ea\u5728\u7279\u5b9a Profile \u4e0b\u53ef\u7528\u3002\u5982\u679c\u5f53\u524d\u6fc0\u6d3b\u7684 Profile \u4e0e\u6240\u9700\u7684 Profile \u4e0d\u5339\u914d\uff0cSpring \u4f1a\u627e\u4e0d\u5230\u5bf9\u5e94\u7684 Bean\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<ul><li>\u68c0\u67e5 <code>application.properties<\/code> \u6216 <code>application.yml<\/code> \u4e2d\u914d\u7f6e\u7684 Profile\uff0c\u786e\u4fdd\u6fc0\u6d3b\u4e86\u6b63\u786e\u7684 Profile\u3002<\/li><li>\u786e\u4fdd <code>@Profile<\/code> \u6ce8\u89e3\u4e0e\u5f53\u524d\u6fc0\u6d3b\u7684 Profile \u76f8\u5339\u914d\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li><\/ul><code>@Profile(\"dev\") @Service public class DevService { \/\/ \u53ea\u5728 dev \u73af\u5883\u4e0b\u63d0\u4f9b } @Profile(\"prod\") @Service public class ProdService { \/\/ \u53ea\u5728 prod \u73af\u5883\u4e0b\u63d0\u4f9b }<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6. <strong>Bean \u7684\u4f5c\u7528\u57df\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1a\u5982\u679c\u4f7f\u7528\u4e86\u4e0d\u540c\u4f5c\u7528\u57df\u7684 Bean\uff08\u5982\u5355\u4f8b\u548c\u539f\u578b\uff09\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cSpring \u53ef\u80fd\u65e0\u6cd5\u6b63\u786e\u89e3\u6790\u9700\u8981\u6ce8\u5165\u7684 Bean\uff0c\u5bfc\u81f4\u627e\u4e0d\u5230\u5bf9\u5e94\u7684 Bean\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<ul><li>\u786e\u4fdd\u6b63\u786e\u914d\u7f6e Bean \u7684\u4f5c\u7528\u57df\u3002\u53ef\u4ee5\u4f7f\u7528 <code>@Scope<\/code> \u6ce8\u89e3\u6307\u5b9a\u4f5c\u7528\u57df\u3002<\/li><li>\u5bf9\u4e8e\u539f\u578b\u4f5c\u7528\u57df\u7684 Bean\uff0c\u786e\u4fdd\u5728\u9700\u8981\u6ce8\u5165\u65f6\u4f7f\u7528\u5408\u9002\u7684\u65b9\u5f0f\u8fdb\u884c\u6ce8\u5165\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li><\/ul><code>@Scope(\"prototype\") \/\/ \u539f\u578b\u4f5c\u7528\u57df @Service public class MyService { }<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">7. <strong>Circular Dependency\uff08\u5faa\u73af\u4f9d\u8d56\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1a\u5faa\u73af\u4f9d\u8d56\u4e5f\u53ef\u80fd\u5bfc\u81f4 Spring \u65e0\u6cd5\u6b63\u786e\u89e3\u6790 Bean\uff0c\u867d\u7136 Spring \u4f1a\u901a\u8fc7\u67d0\u4e9b\u65b9\u5f0f\u89e3\u51b3\u90e8\u5206\u6784\u9020\u5668\u6ce8\u5165\u7684\u5faa\u73af\u4f9d\u8d56\uff0c\u4f46\u5982\u679c Bean \u7684\u914d\u7f6e\u4e0d\u5f53\uff0c\u53ef\u80fd\u4ecd\u4f1a\u5bfc\u81f4 <code>NoSuchBeanDefinitionException<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong>\uff1a<ul><li>\u4f7f\u7528 <code>@Lazy<\/code> \u6ce8\u89e3\u6765\u5ef6\u8fdf\u52a0\u8f7d Bean\uff0c\u907f\u514d\u5728\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\u4ea7\u751f\u5faa\u73af\u4f9d\u8d56\u3002 <strong>\u793a\u4f8b<\/strong>\uff1a<\/li><\/ul><code>@Autowired @Lazy private MyService myService; \/\/ \u5ef6\u8fdf\u52a0\u8f7d\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u603b\u7ed3\u4e0e\u89e3\u51b3\u65b9\u6cd5<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u786e\u4fdd Bean \u5df2\u6b63\u786e\u6ce8\u518c<\/strong>\uff1a\u4f7f\u7528 <code>@ComponentScan<\/code> \u6216 XML \u914d\u7f6e\u6b63\u786e\u626b\u63cf Bean\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u67e5 Bean \u540d\u79f0<\/strong>\uff1a\u786e\u8ba4\u8bf7\u6c42\u7684 Bean \u540d\u79f0\u4e0e\u5bb9\u5668\u4e2d\u6ce8\u518c\u7684\u540d\u79f0\u4e00\u81f4\u3002<\/li>\n\n\n\n<li><strong>\u4fee\u6b63\u4f9d\u8d56\u6ce8\u5165<\/strong>\uff1a\u4f7f\u7528 <code>@Autowired<\/code> \u548c <code>@Qualifier<\/code> \u6ce8\u89e3\u65f6\u786e\u4fdd\u6ce8\u5165\u7684 Bean \u5b58\u5728\u4e14\u7c7b\u578b\u5339\u914d\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u67e5\u914d\u7f6e\u6587\u4ef6\u548c Profile<\/strong>\uff1a\u786e\u4fdd\u6b63\u786e\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\uff0c\u6fc0\u6d3b\u6b63\u786e\u7684 Profile\u3002<\/li>\n\n\n\n<li><strong>\u4f5c\u7528\u57df\u4e0e\u5faa\u73af\u4f9d\u8d56<\/strong>\uff1a\u907f\u514d\u5faa\u73af\u4f9d\u8d56\uff0c\u4f7f\u7528 <code>@Lazy<\/code> \u6216\u8c03\u6574\u8bbe\u8ba1\u6765\u907f\u514d\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><code>NoSuchBeanDefinitionException<\/code> \u662f Spring \u4e2d\u5e38\u89c1\u7684\u5f02\u5e38\uff0c\u7406\u89e3\u5176\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u6392\u67e5\u548c\u4fee\u590d Bean \u6ce8\u5165\u95ee\u9898\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring \u4e2d\uff0cNoSuchBeanDefinitionException \u9519\u8bef\u901a\u5e38\u8868\u793a\u5bb9\u5668\u4e2d\u627e\u4e0d\u5230\u6240\u8bf7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-431","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/431\/revisions\/432"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}