{"id":202,"date":"2024-08-15T12:30:49","date_gmt":"2024-08-15T04:30:49","guid":{"rendered":"https:\/\/note.ui11.cc\/?p=202"},"modified":"2024-08-15T12:47:33","modified_gmt":"2024-08-15T04:47:33","slug":"configuration","status":"publish","type":"post","link":"https:\/\/note.ui11.cc\/index.php\/2024\/08\/15\/configuration\/","title":{"rendered":"@Configuration"},"content":{"rendered":"\n<p><code>@Configuration<\/code>&nbsp;\u662fSpring\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u6ce8\u89e3\uff0c\u7528\u4e8e\u6307\u793a\u4e00\u4e2a\u7c7b\u4f5c\u4e3aSpring\u914d\u7f6e\u7c7b\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53Spring\u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u68c0\u6d4b\u5e26\u6709&nbsp;<code>@Configuration<\/code>&nbsp;\u6ce8\u89e3\u7684\u7c7b\uff0c\u5e76\u5c06\u8fd9\u4e9b\u7c7b\u89c6\u4e3a\u914d\u7f6e\u6e90\uff0c\u4ece\u4e2d\u521b\u5efa\u548c\u7ba1\u7406Bean\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u952e\u70b9\u89e3\u91ca\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u914d\u7f6e\u7c7b<\/strong>\uff1a\u5e26\u6709&nbsp;<code>@Configuration<\/code>&nbsp;\u6ce8\u89e3\u7684\u7c7b\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a&nbsp;<code>@Bean<\/code>&nbsp;\u6ce8\u89e3\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5b9a\u4e49\u4e86Spring\u5bb9\u5668\u4e2d\u5c06\u8981\u521b\u5efa\u548c\u7ba1\u7406\u7684Bean\u3002<\/li>\n\n\n\n<li><strong>Bean\u7684\u521b\u5efa<\/strong>\uff1a\u5f53Spring\u5bb9\u5668\u5904\u7406\u914d\u7f6e\u7c7b\u65f6\uff0c\u5b83\u4f1a\u8c03\u7528\u8fd9\u4e9b&nbsp;<code>@Bean<\/code>&nbsp;\u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u5e76\u5c06\u8fd4\u56de\u7684\u5bf9\u8c61\u6ce8\u518c\u4e3aSpring\u7ba1\u7406\u7684Bean\u3002<\/li>\n\n\n\n<li><strong>\u4f9d\u8d56\u6ce8\u5165<\/strong>\uff1a\u914d\u7f6e\u7c7b\u672c\u8eab\u4e5f\u53ef\u4ee5\u88abSpring\u7ba1\u7406\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u53ef\u4ee5\u6ce8\u5165\u5176\u4ed6Bean\uff0c\u4e5f\u53ef\u4ee5\u88ab\u5176\u4ed6Bean\u6ce8\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f<\/h3>\n\n\n\n<p><code>@Configuration<\/code>&nbsp;\u6ce8\u89e3\u901a\u5e38\u7528\u4e8e\u5b9a\u4e49\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\uff0c\u5305\u62ec\u670d\u52a1\u5c42\u3001\u6570\u636e\u8bbf\u95ee\u5c42\u3001\u5de5\u5177\u7c7b\u7b49\u7684Bean\u5b9a\u4e49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b<\/h3>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u670d\u52a1\u7c7b&nbsp;<code>UserService<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>@Configuration<\/code>&nbsp;\u6ce8\u89e3\u6765\u5b9a\u4e49\u5b83\u7684Bean\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class AppConfig {\n    \n    @Bean\n    public UserService userService() {\n        return new UserServiceImpl();\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cAppConfig \u7c7b\u88ab\u6807\u8bb0\u4e3a @Configuration\uff0c\u5e76\u4e14\u5b9a\u4e49\u4e86\u4e00\u4e2a userService \u7684Bean\u3002\u5f53Spring\u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u5b83\u4f1a\u521b\u5efa UserService \u7684\u5b9e\u4f8b\uff0c\u5e76\u5c06\u5176\u6ce8\u518c\u4e3a\u4e00\u4e2aBean\u3002<br><br>\u6ce8\u610f\u4e8b\u9879<br>@Configuration \u7c7b\u672c\u8eab\u4e5f\u662f\u4e00\u4e2aBean\uff0c\u56e0\u6b64\u5b83\u4eec\u53ef\u4ee5\u4f7f\u7528 @Autowired \u6216\u5176\u4ed6\u4f9d\u8d56\u6ce8\u5165\u6ce8\u89e3\u6765\u6ce8\u5165\u5176\u4ed6Bean\u3002<br>\u5728\u4f7f\u7528 @Configuration \u65f6\uff0c\u786e\u4fdd\u4f60\u7684\u7c7b\u4f4d\u4e8eSpring\u7684\u7ec4\u4ef6\u626b\u63cf\u8def\u5f84\u4e0b\uff0c\u6216\u8005\u5728\u914d\u7f6e\u7c7b\u4e2d\u660e\u786e\u6307\u5b9a\u8981\u626b\u63cf\u7684\u5305\u3002<br>\u4e3a\u4e86\u907f\u514d\u5faa\u73af\u4f9d\u8d56\uff0c\u786e\u4fdd @Configuration \u7c7b\u4e2d\u7684Bean\u6ce8\u5165\u662f\u901a\u8fc7\u6784\u9020\u5668\u6ce8\u5165\u6216 @PostConstruct \u65b9\u6cd5\u6765\u5b8c\u6210\u7684\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u5b57\u6bb5\u6ce8\u5165\u3002<br>\u901a\u8fc7\u4f7f\u7528 @Configuration \u6ce8\u89e3\uff0c\u4f60\u53ef\u4ee5\u5c06Java\u7c7b\u8f6c\u6362\u4e3aSpring\u914d\u7f6e\u6e90\uff0c\u4ece\u800c\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u548cBean\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u3002<br><br><\/pre>\n\n\n\n<p>\u4f7f\u7528@Configuration\u6ce8\u89e3\u7684\u7c7b\u4e3a\u4f55\u4e0d\u9700\u8981@Component?<\/p>\n\n\n\n<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c<code>@Configuration<\/code>&nbsp;\u6ce8\u89e3\u548c&nbsp;<code>@Component<\/code>&nbsp;\u6ce8\u89e3\u867d\u7136\u90fd\u7528\u4e8e\u6807\u8bb0\u7c7b\uff0c\u4f46\u5b83\u4eec\u7684\u7528\u9014\u548c\u542b\u4e49\u6709\u6240\u4e0d\u540c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>@Configuration<\/code>&nbsp;\u6ce8\u89e3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>@Configuration<\/code>\u00a0\u6ce8\u89e3\u7528\u4e8e\u6807\u8bb0\u4e00\u4e2a\u7c7b\u4f5c\u4e3aSpring\u7684\u914d\u7f6e\u7c7b\u3002\u8fd9\u610f\u5473\u7740\u8fd9\u4e2a\u7c7b\u4e2d\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u00a0<code>@Bean<\/code>\u00a0\u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5b9a\u4e49\u4e86Spring\u5bb9\u5668\u4e2d\u5c06\u8981\u521b\u5efa\u548c\u7ba1\u7406\u7684Bean\u3002<\/li>\n\n\n\n<li>\u5f53Spring\u5bb9\u5668\u5904\u7406\u5e26\u6709\u00a0<code>@Configuration<\/code>\u00a0\u6ce8\u89e3\u7684\u7c7b\u65f6\uff0c\u5b83\u4f1a\u4f7f\u7528CGLIB\u5e93\u6765\u589e\u5f3a\u8fd9\u4e2a\u7c7b\uff0c\u4f7f\u5f97\u8fd9\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u4f5c\u4e3a\u5de5\u5382\u6765\u521b\u5efa\u5176\u4ed6Bean\u3002\u56e0\u6b64\uff0c<code>@Configuration<\/code>\u00a0\u7c7b\u672c\u8eab\u4e5f\u662f\u4e00\u4e2aBean\u3002<\/li>\n\n\n\n<li><code>@Configuration<\/code>\u00a0\u7c7b\u901a\u5e38\u7528\u4e8e\u5b9a\u4e49\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\uff0c\u5305\u62ec\u670d\u52a1\u5c42\u3001\u6570\u636e\u8bbf\u95ee\u5c42\u3001\u5de5\u5177\u7c7b\u7b49\u7684Bean\u5b9a\u4e49\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><code>@Component<\/code>&nbsp;\u6ce8\u89e3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>@Component<\/code>\u00a0\u662f\u4e00\u4e2a\u901a\u7528\u7684\u6784\u9020\u578b\u6ce8\u89e3\uff0c\u7528\u4e8e\u6307\u793aSpring\u81ea\u52a8\u68c0\u6d4b\u8fd9\u4e2a\u7c7b\uff0c\u5e76\u5c06\u5176\u6ce8\u518c\u4e3a\u4e00\u4e2aBean\u3002<code>@Component<\/code>\u00a0\u6ce8\u89e3\u53ef\u4ee5\u7528\u4e8e\u670d\u52a1\u5c42\uff08<code>@Service<\/code>\uff09\u3001\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\uff08<code>@Repository<\/code>\uff09\u3001\u63a7\u5236\u5668\uff08<code>@Controller<\/code>\uff09\u4ee5\u53ca\u4efb\u4f55\u5176\u4ed6\u9700\u8981\u88abSpring\u7ba1\u7406\u7684\u7ec4\u4ef6\u3002<\/li>\n\n\n\n<li><code>@Component<\/code>\u00a0\u6ce8\u89e3\u901a\u5e38\u7528\u4e8e\u90a3\u4e9b\u6ca1\u6709\u7279\u5b9a\u89d2\u8272\u7684\u666e\u901a\u7c7b\uff0c\u5982\u670d\u52a1\u5c42\u3001\u6570\u636e\u8bbf\u95ee\u5bf9\u8c61\u6216\u5de5\u5177\u7c7b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u4f7f\u7528&nbsp;<code>@Configuration<\/code>&nbsp;\u6ce8\u89e3\u7684\u7c7b\u4e0d\u9700\u8981&nbsp;<code>@Component<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4e00\u4e2a\u7c7b\u88ab\u6807\u8bb0\u4e3a\u00a0<code>@Configuration<\/code>\u00a0\u65f6\uff0c\u5b83\u5df2\u7ecf\u9690\u542b\u5730\u88ab\u6807\u8bb0\u4e3a\u00a0<code>@Component<\/code>\u3002\u8fd9\u662f\u56e0\u4e3a\u00a0<code>@Configuration<\/code>\u00a0\u7c7b\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u00a0<code>@Component<\/code>\uff0c\u5b83\u88abSpring\u5bb9\u5668\u7279\u522b\u5904\u7406\uff0c\u7528\u4e8e\u5b9a\u4e49\u548c\u521b\u5efa\u5176\u4ed6Bean\u3002<\/li>\n\n\n\n<li><code>@Configuration<\/code>\u00a0\u7c7b\u5728\u88abSpring\u5bb9\u5668\u5904\u7406\u65f6\uff0c\u4f1a\u901a\u8fc7CGLIB\u8fdb\u884c\u7c7b\u589e\u5f3a\uff0c\u4f7f\u5f97\u8fd9\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u53ef\u4ee5\u4f5c\u4e3a\u5de5\u5382\u6765\u521b\u5efa\u5176\u4ed6Bean\u3002\u56e0\u6b64\uff0c<code>@Configuration<\/code>\u00a0\u7c7b\u4e0d\u9700\u8981\u989d\u5916\u7684\u00a0<code>@Component<\/code>\u00a0\u6ce8\u89e3\u6765\u88abSpring\u5bb9\u5668\u8bc6\u522b\u548c\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class AppConfig {\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>AppConfig<\/code>&nbsp;\u7c7b\u88ab\u6807\u8bb0\u4e3a&nbsp;<code>@Configuration<\/code>\uff0c\u5b83\u9690\u542b\u5730\u4e5f\u662f\u4e00\u4e2a&nbsp;<code>@Component<\/code>\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u518d\u663e\u5f0f\u5730\u6dfb\u52a0&nbsp;<code>@Component<\/code>&nbsp;\u6ce8\u89e3\u3002<\/p>\n\n\n\n<p>\u603b\u7ed3\u6765\u8bf4\uff0c<code>@Configuration<\/code>&nbsp;\u6ce8\u89e3\u7684\u7c7b\u4e0d\u9700\u8981&nbsp;<code>@Component<\/code>&nbsp;\u6ce8\u89e3\uff0c\u56e0\u4e3a&nbsp;<code>@Configuration<\/code>&nbsp;\u5df2\u7ecf\u63d0\u4f9b\u4e86&nbsp;<code>@Component<\/code>&nbsp;\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u8fd8\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u914d\u7f6e\u7c7b\u5904\u7406\u80fd\u529b\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>@Configuration&nbsp;\u662fSpring\u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u6ce8\u89e3\uff0c\u7528\u4e8e\u6307\u793a\u4e00\u4e2a\u7c7b\u4f5c\u4e3aSpring\u914d\u7f6e\u7c7b\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,4],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-javase","category-mybatis","category-4"],"_links":{"self":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":2,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":204,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/202\/revisions\/204"}],"wp:attachment":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}