{"id":10845,"date":"2023-04-20T22:53:32","date_gmt":"2023-04-20T14:53:32","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10845"},"modified":"2023-04-20T22:56:40","modified_gmt":"2023-04-20T14:56:40","slug":"configuration%e6%b3%a8%e8%a7%a3%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10845","title":{"rendered":"@Configuration\u6ce8\u89e3\u4f7f\u7528"},"content":{"rendered":"\n<p><strong>1\u3001\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ol><li>@<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Configuration&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Configuration<\/a>\u662fSpring\u7684\u6ce8\u89e3\uff0c\u4e0d\u662fSpringBoot\u7684\uff01\u65e9\u5728Spring\u6846\u67b6\u7684\u65f6\u5019\u5c31\u6709\u4f7f\u7528\uff0c\u4f46\u662f\u7531\u4e8e\u90a3\u4e2a\u65f6\u5019\u914d\u7f6e\u6587\u4ef6\u8fd8\u662f\u6bd4\u8f83\u6d41\u884c\uff0c\u56e0\u6b64@Configuration\u6ce8\u89e3\u5e76\u6ca1\u6709\u592a\u76db\u884c\uff0c\u751a\u81f3\u5f88\u591a\u4eba\u5c31\u8ba4\u4e3a\u5b83\u662fSpringBoot\u7684\u6ce8\u89e3\u3002<\/li><\/ol>\n\n\n\n<p>2. @Configuration\u6ce8\u89e3\u7684\u4f5c\u7528\uff1a\u58f0\u660e\u4e00\u4e2a\u7c7b\u4e3a\u914d\u7f6e\u7c7b\uff0c\u7528\u4e8e\u53d6\u4ee3bean.xml\u914d\u7f6e\u6587\u4ef6\u6ce8\u518cbean\u5bf9\u8c61\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=\"\">@Target(ElementType.TYPE)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Component\npublic @interface Configuration {\n\n\t@AliasFor(annotation = Component.class)\n\tString value() default \"\";\n\n\tboolean proxyBeanMethods() default true;\n<\/pre>\n\n\n\n<ul><li>\u5e95\u5c42\u4ee3\u7801\u5c31\u4e24\u4e2a\u5c5e\u6027\uff0c\u4e00\u4e2a\u7528\u4e8e\u58f0\u660e\u914d\u7f6e\u7c7b\u7684\u540d\u79f0\uff0c\u4e00\u4e2a\u7528\u4e8e\u58f0\u660e\u662f\u5426\u662f\u4ee3\u7406\u5bf9\u8c61\u65b9\u6cd5\uff08\u91cd\u70b9\uff09\u3002<\/li><li>\u7531\u4e8e\u6709@Component\u6ce8\u89e3\u7684\u52a0\u6301\uff0c\u90a3\u4e48\u88ab\u58f0\u660e\u7684\u914d\u7f6e\u7c7b\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u7ec4\u4ef6\uff01<\/li><\/ul>\n\n\n\n<p><strong>2\u3001\u57fa\u7840\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>@Configuration\u6ce8\u89e3\u5e38\u5e38\u4e00\u8d77\u642d\u914d\u4f7f\u7528\u7684\u6ce8\u89e3\u6709@Bean\u3001@Scope\u3001@Lazy\u4e09\u4e2a\u6bd4\u8f83\u5e38\u89c1\uff1a<\/p>\n\n\n\n<ul><li>@Bean\uff1a\u7b49\u4ef7\u4e8eSpring\u4e2d\u7684bean\u6807\u7b7e\u7528\u4e8e\u6ce8\u518cbean\u5bf9\u8c61\u7684\uff0c\u5185\u90e8\u6709\u4e00\u4e9b\u521d\u59cb\u5316\u3001\u9500\u6bc1\u7684\u5c5e\u6027\u2026<\/li><li>@Scope\uff1a\u7528\u4e8e\u58f0\u660e\u8be5bean\u7684\u4f5c\u7528\u57df\uff0c\u4f5c\u7528\u57df\u6709singleton\u3001prototype\u3001request\u3001session\u3002<\/li><li>@Lazy\uff1a\u6807\u8bb0\u8be5bean\u662f\u5426\u5f00\u542f\u61d2\u52a0\u8f7d\u3002<\/li><\/ul>\n\n\n\n<p>\u8fd9\u91cc\u51c6\u5907User\u548cDog\u4e24\u4e2a\u7c7b<\/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=\"\">@Data\n@AllArgsConstructor\n@NoArgsConstructor\npublic class User {\n    private String name;\n    private int age;\n    private Dog dog;\n}\n<\/pre>\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=\"\">@Data\n@NoArgsConstructor\n@AllArgsConstructor\npublic class Dog {\n    private String name;\n    private int age;\n}\n<\/pre>\n\n\n\n<p><strong>2.1\u3001@Bean\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<p>\u5148\u6765\u770b\u770b\u5982\u4f55\u58f0\u660e\u914d\u7f6e\u4e00\u4e2abean\u5bf9\u8c61\u7684\u6ce8\u518c<\/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=\"\">@Configuration\npublic class MyConfig {\n\n    @Bean(\"user\")\n    public User getUser(){\n    \tSystem.out.println(\"User\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new User(\"\u7528\u6237\", 22, getDog());\n    }\n\n    @Bean(\"dog\")\n    public Dog getDog(){\n    \tSystem.out.println(\"Dog\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new Dog(\"\u91d1\u6bdb\", 3);\n    }\n}\n<\/pre>\n\n\n\n<ul><li>\u8fd9\u6837\u5c31\u58f0\u660e\u914d\u7f6e\u4e86\u4e24\u4e2abean\uff0c\u5728Spring\u4e2d\u76f8\u5f53\u4e8e\u6ce8\u518c\u5230xml\u914d\u7f6e\u6587\u4ef6\u4e2d<\/li><li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u4ee5<strong>\u997f\u6c49\u5355\u4f8b<\/strong>\u7684\u5f62\u5f0f\u8fdb\u884c\u521b\u5efa\uff0c\u5373IOC\u5bb9\u5668\u521b\u5efa\u65f6\u7acb\u5373\u521b\u5efa\u8fd9\u4e24\u4e2abean\u3002<\/li><\/ul>\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\npublic class MainApplication {\n\n    public static void main(String[] args) {\n        \/\/ \u83b7\u53d6IOC\u5bb9\u5668\n        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);\n        System.out.println(\"====================================================\");\n        User user = run.getBean(\"user\", User.class);\n        System.out.println(user.toString());\n        Dog dog = run.getBean(\"dog\", Dog.class);\n        System.out.println(dog.toString());\n        System.out.println(\"====================================================\");\n    }\n\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-264-1024x390.png\" alt=\"\" class=\"wp-image-10847\" width=\"437\" height=\"165\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-264-1024x390.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-264-300x114.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-264-768x292.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-264-830x316.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-264-230x88.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-264-350x133.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-264.png 1124w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2.2\u3001@Scope\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<p>\u4e4b\u524d\u901a\u8fc7\u7b80\u5355\u7684bean\u521b\u5efa\u53ef\u4ee5\u770b\u5230\u8fd9\u4e9bbean\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u90fd\u662f\u997f\u6c49\u7684\u5f62\u5f0f\u52a0\u8f7d\uff0c\u5e76\u6ca1\u6709\u770b\u51fa\u662f\u5355\u4f8b\u7684\u5f62\u5f0f\u3002\u8fd9\u91cc\u4f7f\u7528Scope\u6ce8\u89e3\u53ef\u4ee5\u8fdb\u884c\u914d\u7f6e\uff01<\/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=\"\">@Configuration\npublic class MyConfig {\n\n    @Bean(\"user\")\n    @Scope(SCOPE_PROTOTYPE)         \/\/\u591a\u4f8b\n    public User getUser(){\n        System.out.println(\"User\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new User(\"\u7528\u6237\", 22, getDog());\n    }\n\n    @Bean(\"dog\")\n    @Scope(SCOPE_SINGLETON)         \/\/\u5355\u4f8b\n    public Dog getDog(){\n        System.out.println(\"Dog\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new Dog(\"\u91d1\u6bdb\", 3);\n    }\n}\n<\/pre>\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\npublic class MainApplication {\n\n    public static void main(String[] args) {\n        \/\/\u83b7\u5f97IOC\u5bb9\u5668\n        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);\n        \n        System.out.println(\"====================================================\");\n        User user1 = run.getBean(\"user\", User.class);\n        User user2 = run.getBean(\"user\", User.class);\n        System.out.println(\"user1 == user2 ? \" + (user1 == user2));\n        Dog dog1 = run.getBean(\"dog\", Dog.class);\n        Dog dog2 = run.getBean(\"dog\", Dog.class);\n        System.out.println(\"dog1 == dog2 ? \" + (dog1 == dog2));\n        System.out.println(\"====================================================\");\n    }\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-265-1024x448.png\" alt=\"\" class=\"wp-image-10848\" width=\"426\" height=\"186\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265-1024x448.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265-300x131.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265-768x336.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265-830x363.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265-230x101.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265-350x153.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265-480x210.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-265.png 1098w\" sizes=\"(max-width: 426px) 100vw, 426px\" \/><\/figure><\/div>\n\n\n\n<ul><li>User\u5bf9\u8c61\u4f7f\u7528\u591a\u4f8b\u7684\u5f62\u5f0f\u521b\u5efabean\uff0c\u53ea\u8981\u6bcf\u6b21\u4eceIOC\u4e2d\u83b7\u53d6bean\u90fd\u4f1a\u662f\u4e0d\u4e00\u6837\u7684<\/li><li>Dog\u5bf9\u8c61\u91c7\u7528\u5355\u4f8b\u7684\u5f62\u5f0f\u521b\u5efa\uff0c\u4eceIOC\u4e2d\u83b7\u53d6\u90fd\u4f1a\u662f\u4e00\u6837\u7684\u7ed3\u679c\u3002<\/li><li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u6307\u5b9aScope\u65f6\u91c7\u7528\u5355\u4f8b\u7684\u5f62\u5f0f<\/li><li>\u5e76\u4e14\u53ef\u4ee5\u770b\u5230\u5728\u591a\u4f8b\u7684\u60c5\u51b5\u4e0bIOC\u5bb9\u5668\u521b\u5efa\u7684\u65f6\u5019\u4e0d\u4f1a\u5bf9\u591a\u4f8b\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa\uff0c\u800c\u662f\u91c7\u7528\u61d2\u52a0\u8f7d\u7684\u5f62\u5f0f\u5728\u4f7f\u7528\u7684\u65f6\u5019\u521b\u5efa\u3002<br><\/li><\/ul>\n\n\n\n<p><strong>2.3\u3001@Lazy\u6ce8\u89e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u61d2\u52a0\u8f7d\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u662f\u975e\u5e38\u6709\u5fc5\u8981\u7684\uff0c\u5e76\u4e14\u61d2\u52a0\u8f7d\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\u4f9d\u8d56\u5bf9\u8c61\u7684\u61d2\u52a0\u8f7d\u95ee\u9898\uff01<\/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=\"\">@Configuration\npublic class MyConfig {\n\n    @Bean(\"user\")\n    @Lazy(false)        \/\/\u7acb\u5373\u52a0\u8f7d\n    public User getUser(){\n        System.out.println(\"User\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new User();\n        \/\/return new User(\"\u7528\u6237\", 22, getDog());      \/\/\u8fd9\u65f6dog\u7684\u61d2\u52a0\u8f7d\u5931\u6548\uff0c\u56e0\u4e3auser\u662f\u7acb\u5373\u52a0\u8f7d\uff0c\u4f9d\u8d56dog\n    }\n\n    @Bean(\"dog\")\n    @Lazy(true)         \/\/\u61d2\u52a0\u8f7d\n    public Dog getDog(){\n        System.out.println(\"Dog\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new Dog(\"\u91d1\u6bdb\", 3);\n    }\n}\n<\/pre>\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\npublic class MainApplication {\n\n    public static void main(String[] args) {\n        \/\/\u83b7\u5f97IOC\u5bb9\u5668\n        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);\n        \n        System.out.println(\"====================================================\");\n        User user1 = run.getBean(\"user\", User.class);\n        User user2 = run.getBean(\"user\", User.class);\n        System.out.println(\"user1 == user2 ? \" + (user1 == user2));\n        Dog dog1 = run.getBean(\"dog\", Dog.class);\n        Dog dog2 = run.getBean(\"dog\", Dog.class);\n        System.out.println(\"dog1 == dog2 ? \" + (dog1 == dog2));\n        System.out.println(\"====================================================\");\n    }\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-266-1024x438.png\" alt=\"\" class=\"wp-image-10849\" width=\"424\" height=\"181\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266-1024x438.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266-300x128.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266-768x328.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266-830x355.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266-230x98.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266-350x150.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266-480x205.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-266.png 1086w\" sizes=\"(max-width: 424px) 100vw, 424px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u7acb\u5373\u52a0\u8f7d\uff0c\u5373\u997f\u6c49\u5f0f<\/li><li>\u5982\u679cUser\u5bf9\u8c61\u662f\u4e00\u4e2a\u7acb\u5373\u52a0\u8f7d\uff0cDog\u5bf9\u8c61\u662f\u4e00\u4e2a\u61d2\u52a0\u8f7d\uff1b\u4e14User\u521b\u5efa\u65f6\u9700\u8981\u4f9d\u8d56Dog\u5bf9\u8c61\uff0c\u90a3\u4e48Dog\u5bf9\u8c61\u7684\u61d2\u52a0\u8f7d\u5931\u8d25\u4f1a\u53d8\u4e3a\u7acb\u5373\u52a0\u8f7d<\/li><li>\u5982\u679c\u4e0d\u4f9d\u8d56\u90a3\u4e48\u4e0d\u4f1a\u9020\u6210\u5f71\u54cd\u3002<\/li><\/ul>\n\n\n\n<p><strong>3\u3001@Configuration\u6ce8\u89e3\u7684\u5c5e\u6027<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>@Configuration\u6ce8\u89e3\u4e2d\u6709@Component\u6ce8\u89e3\u7684\u52a0\u6301\uff0c\u56e0\u6b64\u5b83\u81ea\u5df1\u672c\u8eab\u4e5f\u662f\u4e00\u4e2abean\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7Context\u7684\u8fdb\u884c\u83b7\u53d6\u3002<\/li><li>@Configuration\u4e2d\u7684\u5c5e\u6027proxyBeanMethods\u662f\u53ca\u5176\u91cd\u8981\u7684\uff0c\u8bbe\u7f6etrue\/false\u4f1a\u5f97\u5230\u4e0d\u540c\u7684\u6548\u679c\u3002<\/li><\/ul>\n\n\n\n<p><strong>3.1\u3001proxyBeanMethods\u5c5e\u6027\u503c<\/strong><\/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=\"\">@Configuration(proxyBeanMethods = false)\npublic class MyConfig {\n}\nMyConfig bean = run.getBean(MyConfig.class);\nSystem.out.println(bean);\n\/*\n\t\u8f93\u51fa: com.splay.config.MyConfig@c9d82f9\n*\/\n\n@Configuration(proxyBeanMethods = true)\npublic class MyConfig {\n}\nMyConfig bean = run.getBean(MyConfig.class);\nSystem.out.println(bean);\n\/*\n\t\u8f93\u51fa: com.splay.config.MyConfig$$EnhancerBySpringCGLIB$$eefc6c50@3605c4d3\n*\/\n<\/pre>\n\n\n\n<ul><li>\u503c\u4e3afalse\uff1a\u90a3\u4e48MyConfig\u7c7b\u662f\u4e00\u4e2alite\u7684\u914d\u7f6e\u7c7b\uff0c\u6ca1\u6709\u4ee3\u7406\u529f\u80fd<\/li><li>\u503c\u4e3atrue\uff1a\u8be5\u7c7b\u662f\u4e00\u4e2aFull\u7684\u914d\u7f6e\u7c7b\uff0c\u4f7f\u7528cglib\u4ee3\u7406\uff01<\/li><\/ul>\n\n\n\n<p><strong>3.2\u3001proxyBeanMethods = true<\/strong><\/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=\"\">@Configuration(proxyBeanMethods = true)\npublic class MyConfig {\n\n    @Bean(\"user\")\n    public User getUser(){\n        System.out.println(\"User\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new User();\n        \/\/return new User(\"\u7528\u6237\", 22, getDog());      \/\/\u8fd9\u65f6dog\u7684\u61d2\u52a0\u8f7d\u5931\u6548\uff0c\u56e0\u4e3auser\u662f\u7acb\u5373\u52a0\u8f7d\uff0c\u4f9d\u8d56dog\n    }\n\n    @Bean(\"dog\")\n    public Dog getDog(){\n        System.out.println(\"Dog\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new Dog(\"\u91d1\u6bdb\", 3);\n    }\n}\n<\/pre>\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\npublic class MainApplication {\n\t\/\/ proxyBeanMethods = true\n    public static void main(String[] args) {\n        \/\/ \u83b7\u5f97IOC\u5bb9\u5668\n        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);\n\t\t\n        MyConfig bean = run.getBean(MyConfig.class);\n        System.out.println(bean);\n        User user = run.getBean(User.class);\n        Dog dog = run.getBean(Dog.class);\n        System.out.println(bean.getUser() == user);\n        System.out.println(bean.getDog() == dog);\n        System.out.println(user.getDog() == dog);\n        \n    }\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-267-1024x349.png\" alt=\"\" class=\"wp-image-10850\" width=\"446\" height=\"152\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267-1024x349.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267-300x102.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267-768x262.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267-830x283.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267-230x78.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267-350x119.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267-480x164.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-267.png 1302w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/figure><\/div>\n\n\n\n<p>proxyBeanMethods = true\u7684\u60c5\u51b5\u4e0b\uff1a<\/p>\n\n\n\n<ul><li>\u901a\u8fc7\u914d\u7f6e\u7c7b\u8c03\u7528\u65b9\u6cd5\uff0c\u8fd8\u662f\u901a\u8fc7getBean\u76f4\u63a5\u4eceIOC\u5bb9\u5668\u4e2d\u83b7\u53d6\u5bf9\u8c61\uff0c\u83b7\u53d6\u5230\u7684\u90fd\u662f\u540c\u4e00\u4e2a\uff01\uff08\u5355\u4f8b\uff09<\/li><li>\u5916\u90e8\u65e0\u8bba\u5bf9\u914d\u7f6e\u7c7b\u4e2d\u7684\u8fd9\u4e2a\u7ec4\u4ef6\u6ce8\u518c\u65b9\u6cd5\u8c03\u7528\u591a\u5c11\u6b21\u90fd\u662f\u76f4\u63a5\u6ce8\u518c\u5230\u5bb9\u5668\u4e2d\u7684\u5355\u5b9e\u4f8b\u5bf9\u8c61\uff01<\/li><li>\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5\u65f6SpringBoot\u4f1a\u68c0\u67e5\u8fd9\u4e2abean\u5bf9\u8c61\u662f\u5426\u5728\u5bb9\u5668\u4e2d\uff0c\u4fdd\u6301\u5355\u5b9e\u4f8b\u3002<\/li><\/ul>\n\n\n\n<p><strong>3.2\u3001proxyBeanMethods = false<\/strong><\/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=\"\">@Configuration(proxyBeanMethods = false)\npublic class MyConfig {\n\n    @Bean(\"user\")\n    public User getUser(){\n        System.out.println(\"User\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new User(\"\u7528\u6237\", 22, getDog());\n    }\n\n    @Bean(\"dog\")\n    public Dog getDog(){\n        System.out.println(\"Dog\u5bf9\u8c61\u8fdb\u884c\u521b\u5efa!\");\n        return new Dog(\"\u91d1\u6bdb\", 3);\n    }\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-268-1024x397.png\" alt=\"\" class=\"wp-image-10851\" width=\"437\" height=\"169\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-1024x397.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-300x116.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-768x298.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-1536x595.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-830x322.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-230x89.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-350x136.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268-480x186.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-268.png 1816w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u4e0d\u8fdb\u884c\u68c0\u67e5IOC\u5bb9\u5668\u4e2d\u662f\u5426\u5b58\u5728\uff0c\u800c\u662f\u7b80\u5355\u7684\u8c03\u7528\u65b9\u6cd5\u8fdb\u884c\u521b\u5efa\u5bf9\u8c61\u3002<\/li><li>\u901a\u8fc7\u76f4\u63a5getBean\u65b9\u5f0f\u83b7\u53d6IOC\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u83b7\u53d6\u8fd8\u662f\u4e00\u6837\u7684<\/li><li>\u65e0\u6cd5\u4fdd\u8bc1bean\u7684\u5355\u4e00\uff0c\u5931\u53bb\u4e86\u5355\u4f8b\u7684\u610f\u4e49\uff01<\/li><\/ul>\n\n\n\n<p><strong>4\u3001\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>Full\uff08proxyBeanMethods = true\uff09\uff1a\u8fd9\u79cd\u60c5\u51b5\u4e3b\u8981\u7528\u5728bean\u5bf9\u8c61\u7684\u4f9d\u8d56\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u5b58\u5728\u4e00\u4e2abean\u4f9d\u8d56\u53e6\u4e00\u4e2abean\u65f6\uff0c\u4e00\u822c\u4f1a\u91c7\u7528Full\u6a21\u5f0f<\/li><li>Lite\uff08proxyBeanMethods = false\uff09\uff1a\u5f53\u4e0d\u5b58\u5728bean\u5bf9\u8c61\u7684\u4f9d\u8d56\u95ee\u9898\u65f6\uff0c\u4f1a\u624d\u6709\u8f7b\u91cf\u7ea7\u7684\u914d\u7f6e\uff1b\u53ea\u8981\u4e0d\u901a\u8fc7\u8c03\u7528bean\u65b9\u6cd5\uff0c\u800c\u662fgetBean\u76f4\u63a5\u83b7\u53d6\u7684bean\u4e5f\u662f\u540c\u4e00\u4e2a\u7684\u3002<\/li><li>\u5408\u7406\u4f7f\u7528Scope\u6ce8\u89e3\u3001Lazy\u6ce8\u89e3\uff1b\u4e00\u5b9a\u8981\u7ed3\u5408bean\u4e4b\u95f4\u7684\u4f9d\u8d56\u95ee\u9898\u5408\u7406\u4f7f\u7528\uff0c\u5426\u5219\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u9020\u6210\u6ce8\u89e3\u7684\u4e0d\u751f\u6548.<br><\/li><\/ul>\n\n\n\n<p>\u5728Spring\u6846\u67b6\u4e2d\uff0c@Configuration\u6ce8\u89e3\u662f\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u4f5c\u4e3aSpring Bean\u7684\u914d\u7f6e\u7c7b\u7684\u3002\u5b83\u544a\u8bc9Spring\u5bb9\u5668\u8fd9\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u6216\u591a\u4e2aSpring Bean\u3002\u5f53Spring\u5bb9\u5668\u542f\u52a8\u65f6\uff0c\u5b83\u4f1a\u626b\u63cf\u8fd9\u4e9b\u7c7b\uff0c\u8bfb\u53d6\u5176\u4e2d\u7684@Bean\u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u521b\u5efa\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u5e76\u5c06\u5b83\u4eec\u6ce8\u518c\u5230\u5bb9\u5668\u4e2d\u3002\u56e0\u6b64\uff0c@Configuration\u6ce8\u89e3\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u544a\u8bc9Spring\u5bb9\u5668\u54ea\u4e9b\u7c7b\u548c\u65b9\u6cd5\u5e94\u8be5\u88ab\u6ce8\u518c\u4e3aBean\u3002<\/p>\n\n\n\n<p>\u800c@Autowired\u6ce8\u89e3\u5219\u662f\u7528\u6765\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\uff08DI\uff09\u7684\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u5730\u5c06\u4e00\u4e2aBean\u6ce8\u5165\u5230\u53e6\u4e00\u4e2aBean\u4e2d\uff0c\u4ece\u800c\u89e3\u51b3\u4e86Bean\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u4f7f\u7528@Autowired\u6ce8\u89e3\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u7f16\u5199\u5927\u91cf\u7684\u4ee3\u7801\u6765\u5b9e\u73b0Bean\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u4f7f\u7528@Autowired\u6ce8\u89e3\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\uff0c\u4f46\u662f\u5b83\u5e76\u4e0d\u4f1a\u81ea\u52a8\u5730\u5c06\u6240\u6709\u7684Bean\u90fd\u6ce8\u518c\u5230Spring\u5bb9\u5668\u4e2d\u3002\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u5728Spring\u5bb9\u5668\u4e2d\u521b\u5efa\u4e00\u4e2aBean\uff0c\u5c31\u5fc5\u987b\u4f7f\u7528@Configuration\u6ce8\u89e3\u6765\u5b9a\u4e49\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u4f7f\u7528@Bean\u6ce8\u89e3\u6765\u5b9a\u4e49\u8fd9\u4e2aBean\u3002\u8fd9\u6837Spring\u5bb9\u5668\u624d\u80fd\u8bc6\u522b\u548c\u7ba1\u7406\u8fd9\u4e2aBean\u3002<\/p>\n\n\n\n<p>\u603b\u7684\u6765\u8bf4\uff0c@Configuration\u6ce8\u89e3\u548c@Autowired\u6ce8\u89e3\u90fd\u662fSpring\u6846\u67b6\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6ce8\u89e3\uff0c\u5b83\u4eec\u5206\u522b\u7528\u4e8e\u5b9a\u4e49Bean\u548c\u5b9e\u73b0\u4f9d\u8d56\u6ce8\u5165\u3002\u867d\u7136@Autowired\u6ce8\u89e3\u53ef\u4ee5\u81ea\u52a8\u5730\u5c06Bean\u6ce8\u5165\u5230\u53e6\u4e00\u4e2aBean\u4e2d\uff0c\u4f46\u662f\u4f7f\u7528@Configuration\u6ce8\u89e3\u53ef\u4ee5\u66f4\u52a0\u6e05\u6670\u5730\u5b9a\u4e49Bean\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u66f4\u52a0\u65b9\u4fbf\u5730\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n\n\n\n<p>@Configuration\u6ce8\u89e3\u7684\u4e3b\u8981\u4f5c\u7528\u5728\u4e8e\u5c06Bean\u7684\u5b9a\u4e49\u96c6\u4e2d\u5728\u4e00\u5904\uff0c\u5e76\u63d0\u4f9b\u4e00\u79cd\u6e05\u6670\u3001\u7edf\u4e00\u7684\u65b9\u5f0f\u6765\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u3002\u4f7f\u7528@Configuration\u6ce8\u89e3\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u7684Bean\u5b9a\u4e49\u653e\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u907f\u514d\u4e86Bean\u7684\u6563\u4e71\u5b9a\u4e49\uff0c\u540c\u65f6\u4e5f\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6613\u4e8e\u7ef4\u62a4\u548c\u7ba1\u7406\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u4f7f\u7528@Configuration\u6ce8\u89e3\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684Bean\u4f9d\u8d56\u5173\u7cfb\u548c\u914d\u7f6e\u3002\u901a\u8fc7\u5728@Configuration\u6ce8\u89e3\u7684\u7c7b\u4e2d\u5b9a\u4e49\u591a\u4e2a@Bean\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4e0d\u540c\u7684Bean\u5b9a\u4e49\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5b9e\u73b0\u590d\u6742\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u6b64\u5916\uff0c@Configuration\u6ce8\u89e3\u8fd8\u652f\u6301\u4f7f\u7528@Import\u6ce8\u89e3\u5bfc\u5165\u5176\u4ed6\u914d\u7f6e\u7c7b\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u914d\u7f6e\u7ec4\u5408\u548c\u7ba1\u7406\u3002<\/p>\n\n\n\n<p>\u603b\u4e4b\uff0c@Configuration\u6ce8\u89e3\u662fSpring\u6846\u67b6\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6ce8\u89e3\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u96c6\u4e2d\u5f0f\u3001\u7edf\u4e00\u7684\u65b9\u5f0f\u6765\u7ba1\u7406Bean\u7684\u5b9a\u4e49\u548c\u751f\u547d\u5468\u671f\uff0c\u5e76\u652f\u6301\u66f4\u52a0\u590d\u6742\u7684Bean\u4f9d\u8d56\u5173\u7cfb\u548c\u914d\u7f6e\u3002\u56e0\u6b64\uff0c\u5728Spring\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528@Configuration\u6ce8\u89e3\u6765\u58f0\u660eBean\u662f\u975e\u5e38\u5e38\u89c1\u7684\u505a\u6cd5\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/04\/image-269-1024x822.png\" alt=\"\" class=\"wp-image-10854\" width=\"399\" height=\"320\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269-1024x822.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269-300x241.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269-768x616.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269-830x666.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269-230x185.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269-350x281.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269-480x385.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/04\/image-269.png 1406w\" sizes=\"(max-width: 399px) 100vw, 399px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u524d\u8a00 @Configuration\u662fSpring\u7684\u6ce8\u89e3\uff0c\u4e0d\u662fSpringBoot\u7684\uff01\u65e9\u5728Spring\u6846\u67b6\u7684 [&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\/10845"}],"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=10845"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10845\/revisions"}],"predecessor-version":[{"id":10855,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10845\/revisions\/10855"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}