{"id":365,"date":"2026-09-30T00:07:50","date_gmt":"2026-09-29T16:07:50","guid":{"rendered":"https:\/\/arknight.wiki\/?p=365"},"modified":"2026-09-30T00:07:50","modified_gmt":"2026-09-29T16:07:50","slug":"springboot2-%e5%ba%95%e5%b1%82%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/arknight.wiki\/index.php\/2026\/09\/30\/springboot2-%e5%ba%95%e5%b1%82%e6%b3%a8%e8%a7%a3\/","title":{"rendered":"SpringBoot2 \u5e95\u5c42\u6ce8\u89e3"},"content":{"rendered":"<h1>SpringBoot2 \u5e95\u5c42\u6ce8\u89e3<\/h1>\n<h2>@Configuration\u6ce8\u89e3\uff1a<\/h2>\n<p>Spring\u4e2d\uff1a<br \/>\n<code>bean<\/code> \u5305\u653e\u6570\u636e\u7c7b\uff0c\u6bd4\u5982 <code>User<\/code>\u3001<code>Pet<\/code>\u3002  <\/p>\n<p><code>config<\/code> \u5305\u653e\u914d\u7f6e\u7c7b\uff0c\u6bd4\u5982 <code>MyConfig<\/code>\uff0c\u7528\u6765\u544a\u8bc9 Spring \u521b\u5efa\u54ea\u4e9b\u5bf9\u8c61\u3002<\/p>\n<p><code>@Configuration<\/code>\u6ce8\u89e3\uff1a\u6807\u5728\u7c7b\u4e0a\uff0c\u8868\u793a\u8fd9\u4e2a\u7c7b\u662f\u4e00\u4e2a\u201c\u914d\u7f6e\u7c7b\u201d\uff0c\u5373<strong>\u4e13\u95e8\u7528\u6765\u544a\u8bc9 Spring \u5e94\u8be5\u521b\u5efa\u54ea\u4e9b\u5bf9\u8c61\u3001\u600e\u4e48\u521b\u5efa\u5bf9\u8c61\u3001\u600e\u4e48\u914d\u7f6e\u9879\u76ee\u884c\u4e3a\u7684\u7c7b<\/strong>\u3002<\/p>\n<p><code>@Bean<\/code>\u6ce8\u89e3\uff1a\u6807\u5728\u65b9\u6cd5\u4e0a\uff0c\u8868\u793a\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\u8981\u653e\u8fdb Spring \u5bb9\u5668\u3002\u914d\u7f6e\u7c7b\u672c\u8eab\u4e5f\u5728Spring\u5bb9\u5668\u91cc\u9762\u3002<\/p>\n<p>\u5982\uff1a<\/p>\n<p>\u5728Bean\u5305\u91cc\u65b0\u5efa\uff1a<\/p>\n<p>User\u7c7b\uff1a<\/p>\n<pre><code>package com.inex.Bean;\n\npublic class User {\n    private String name;\n    private Integer age;\n    public User(){\n    }\n    public User(String name,Integer age){\n        this.name = name;\n        this.age = age;\n    }\n    public String getName(){\n        return name;\n    }\n    public Integer getAge(){\n        return age;\n    }\n}\n<\/code><\/pre>\n<p>Pet\u7c7b\uff1a<\/p>\n<pre><code>package com.inex.Bean;\n\npublic class Pet {\n    private String name;\n    private Integer age;\n    public Pet(){}\n    public Pet(String name,Integer age){\n        this.name = name;\n        this.age = age;\n    }\n    public String getName(){\n        return name;\n    }\n    public Integer getAge(){\n        return age;\n    }\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u5728config\u5305\u65b0\u5efaMyconfig\u7c7b\uff1a<\/p>\n<pre><code>package com.inex.config;\n\nimport com.inex.Bean.Pet;\nimport com.inex.Bean.User;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration \/\/\u544a\u8bc9SpringBoot\u8fd9\u662f\u4e00\u4e2a\u914d\u7f6e\u7c7b == \u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\nclass MyConfig {\n\n    @Bean \/\/\u7ed9\u5bb9\u5668\u6dfb\u52a0\u7ec4\u4ef6\uff0c\u4ee5\u65b9\u6cd5\u540d\u4f5c\u4e3a\u7ec4\u4ef6id\uff0c\u8fd4\u56de\u7c7b\u578b\u5c31\u662f\u7ec4\u4ef6\u7c7b\u578b\uff0c\u8fd4\u56de\u7684\u503c\u5c31\u662f\u7ec4\u4ef6\u5728\u5bb9\u5668\u4e2d\u7684\u5b9e\u4f8b\n    public User user01(){\n        return new User(\"inex\",18);\n    }\n\n    @Bean(\"Jerry\") \/\/\u4e5f\u53ef\u4ee5\u8fd9\u6837\u81ea\u5b9a\u4e49id\n    public Pet miaomiao(){\n        return new Pet(\"Tom\",3);\n    }\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u5f97\u77e5\uff0c\u6211\u4eec\u4eceBean\u5bb9\u5668\u91cc\u901a\u8fc7id\u548c\u7c7b\u578b\u62ff\u5230\u7684\u4e00\u76f4\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<pre><code>package com.inex;\n\nimport com.inex.Bean.Pet;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.context.ConfigurableApplicationContext;\n\n@SpringBootApplication\npublic class MainApplication {\n    public static void main (String[] args){\n        \/\/ 1\u3001\u8fd4\u56de\u6211\u4eec\u7684IOC\u5bb9\u5668\n        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);\n\n        \/\/ 2\u3001\u67e5\u770b\u5bb9\u5668\u91cc\u9762\u7684\u7ec4\u4ef6\n        String[] names = run.getBeanDefinitionNames();\n        for (String name : names) {\n            System.out.println(name);\n        }\n\n        \/\/ 3. \u4ece\u5bb9\u5668\u4e2d\u83b7\u53d6\u7ec4\u4ef6\n        Pet tom01 = run.getBean(\"Jerry\", Pet.class);\n        Pet tom02 = run.getBean(\"Jerry\", Pet.class);\n        System.out.println(\"\u7ec4\u4ef6\uff1a\" + (tom01 == tom02));\n    }\n}<\/code><\/pre>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b<code>@Configuration(proxyBeanMethods = true)<\/code>\uff0c\u4e5f\u53eb <strong>Full \u6a21\u5f0f<\/strong>\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cSpring \u4f1a\u7ed9\u914d\u7f6e\u7c7b\u521b\u5efa\u4e00\u4e2a<strong>\u4ee3\u7406\u5bf9\u8c61<\/strong>\u3002\u5982\u679c\u4f60\u8c03\u7528\u4e00\u4e2a <code>@Bean<\/code> \u65b9\u6cd5\uff1a <\/p>\n<pre><code>zhangsan.setPet(miaomiao());\n\u6216\u8005\u5728\u914d\u7f6e\u7c7b\u5916\uff1a\nPet pet01 = Myconfig.miaomiao();<\/code><\/pre>\n<p>Spring \u4f1a\u62e6\u622a\u8fd9\u4e2a\u8c03\u7528\uff0c\u4e0d\u662f\u771f\u7684\u91cd\u65b0\u6267\u884c <code>miaomiao()<\/code>\uff0c\u800c\u662f\u4ece\u5bb9\u5668\u91cc\u62ff\u5df2\u7ecf\u521b\u5efa\u597d\u7684\u90a3\u4e2a <code>Pet<\/code> \u5bf9\u8c61\u3002<\/p>\n<p>\u53ef\u4ee5\u8bbe\u7f6e<code>@Configuration(proxyBeanMethods = false)<\/code>\uff0c\u4e5f\u53eb <strong>Lite \u6a21\u5f0f<\/strong>\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cSpring \u4e0d\u4f1a\u4ee3\u7406\u8fd9\u4e9b <code>@Bean<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u8fd9\u65f6\uff1a<\/p>\n<pre><code>zhangsan.setPet(miaomiao());<\/code><\/pre>\n<p>\u5c31\u662f\u4e00\u6b21\u666e\u901a Java \u65b9\u6cd5\u8c03\u7528\uff0c\u4f1a\u771f\u7684\u6267\u884c\uff1a<\/p>\n<pre><code>return new Pet(\"Tom\",3);<\/code><\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f1a\u65b0\u521b\u5efa\u4e00\u4e2a <code>Pet<\/code> \u5bf9\u8c61\u3002<\/p>\n<p>\u540c\u65f6\uff0cSpring \u5bb9\u5668\u81ea\u5df1\u4e5f\u4f1a\u6ce8\u518c\u4e00\u4e2a\u53eb <code>\"Jerry\"<\/code> \u7684 <code>Pet<\/code> \u5bf9\u8c61\u3002<\/p>\n<p><code>proxyBeanMethods = true<\/code> \u9002\u5408\uff1a<strong>\u914d\u7f6e\u7c7b\u91cc\u7684 Bean \u65b9\u6cd5\u4e4b\u95f4\u6709\u4e92\u76f8\u8c03\u7528\u3001\u4e92\u76f8\u4f9d\u8d56\u3002<\/strong><\/p>\n<p><code>proxyBeanMethods = false<\/code> \u9002\u5408\uff1a<strong>\u914d\u7f6e\u7c7b\u91cc\u7684 Bean \u65b9\u6cd5\u4e92\u76f8\u72ec\u7acb\uff0c\u4e0d\u4e92\u76f8\u8c03\u7528\u3002\u8fd9\u6837\u542f\u52a8\u4f1a\u66f4\u5feb\u901f<\/strong><\/p>\n<h2><code>@Import<\/code>\u6ce8\u89e3\uff1a<\/h2>\n<p>\u4f7f\u7528\u683c\u5f0f\uff1a<\/p>\n<pre><code>@Import({User.class, DBHelper.class})<\/code><\/pre>\n<p>\u5b83\u7684\u4f5c\u7528\u662f\uff1a<strong>\u628a\u6307\u5b9a\u7684\u7c7b\u5bfc\u5165\u5230 Spring \u5bb9\u5668\u4e2d\uff0c\u8ba9 Spring \u81ea\u52a8\u521b\u5efa\u8fd9\u4e9b\u7c7b\u7684\u5bf9\u8c61\u3002\u5bf9\u8c61\u540d\u9ed8\u8ba4\u5c31\u662f\u7c7b\u7684\u5168\u9650\u5b9a\u540d<\/strong><\/p>\n<p>\u4e00\u822c\u5199\u5728\u914d\u7f6e\u7c7b\u91cc\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5bfc\u5165\u7684\u662f\u8fd9\u4e2a\u914d\u7f6e\u7c7b\u672c\u8eab<code>@Bean<\/code> \u65b9\u6cd5\u521b\u5efa\u7684\uff0c\u90a3\u8fd8\u662f\u4f1a\u51fa\u73b0\u4e24\u4e2a\u4e0d\u540c\u7684\u5bf9\u8c61<\/p>\n<p>\u4f8b\uff1a<\/p>\n<pre><code>@Configuration\n@Import(User.class)\npublic class MyConfig {\n\n    @Bean\n    public User user01() {\n        return new User(\"zhangsan\", 18);\n    }\n}<\/code><\/pre>\n<p>\u9a8c\u8bc1\u4ee3\u7801\uff1a<\/p>\n<pre><code>String[] beanNamesForType = run.getBeanNamesForType(User.class);\nSystem.out.println(\"======\");\nfor (String s : beanNamesForType) {\n    System.out.println(s);\n}<\/code><\/pre>\n<h2><code>@Conditional<\/code>\u6ce8\u89e3\uff1a<\/h2>\n<p>\u4f5c\u7528\uff1a\u6ee1\u8db3\u67d0\u79cd\u6761\u4ef6\u624d\u628a\u7ec4\u4ef6\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u91cc\uff0c\u4e0d\u6ee1\u8db3\u6761\u4ef6\u5c31\u5f53\u8fd9\u6bb5\u914d\u7f6e\u4e0d\u5b58\u5728\u3002\u6709\u5f88\u591a\u4e0b\u5c5e\u6ce8\u89e3\uff0c\u6bd4\u5982<code>@ConditionalOnBean<\/code>\u5c31\u662f\u8981\u5728\u5bb9\u5668\u91cc\u6709\u7b26\u5408\u67d0\u6761\u4ef6\u65f6\u624d\u6ce8\u5165\u8be5\u6ce8\u89e3\u4e0b\u7684\u5185\u5bb9<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code>    @Bean\n    @ConditionalOnBean(name = \"tomcatPet\")\n    public User user01() {\n        return new User(\"zhangsan\", 18);\n    }<\/code><\/pre>\n<p>\u5c31\u662f\u8981\u5728\u5bb9\u5668\u91cc\u6709\u53eb<code>tomcatPet<\/code>\u7684\u5bf9\u8c61\u65f6\u624d\u7ee7\u7eed\u6ce8\u5165\u8be5\u6ce8\u89e3\u4e0b\u7684\u5185\u5bb9<\/p>\n<h2><code>@ImportResource<\/code>\u6ce8\u89e3\uff1a<\/h2>\n<p>\u628a\u4f20\u7edf Spring \u7684 XML \u914d\u7f6e\u6587\u4ef6\u5bfc\u5165\u5230 Spring \u5bb9\u5668\u4e2d\u3002<\/p>\n<p>\u4e3b\u8981\u7528\u4e8e\uff1a\u6709 <code>beans.xml<\/code> \u8fd9\u79cd\u914d\u7f6e\u6587\u4ef6\uff0c\u73b0\u5728\u9879\u76ee\u6362\u6210 Spring Boot \u4e86\uff0c\u4f46\u8fd8\u60f3\u7ee7\u7eed\u4f7f\u7528 XML \u91cc\u7684 Bean \u914d\u7f6e\uff0c\u5c31\u53ef\u4ee5\u7528\u8fd9\u4e2a\u6ce8\u89e3\uff0c\u901a\u8fc7\u8def\u5f84\u5c06XML\u7684Bean\u914d\u7f6e\u5199\u8fdb\u5bb9\u5668\uff0c\u4e00\u822c\u5199\u5728\u4e3b\u7c7b\u548c\u914d\u7f6e\u7c7b\u91cc\u9762<\/p>\n<p>\u4f8b\uff1a<\/p>\n<pre><code>@ImportResource(\"classpath:beans.xml\")\n@SpringBootApplication\npublic class MainApplication {\n    public static void main(String[] args) {\n        ConfigurableApplicationContext run =\n                SpringApplication.run(MainApplication.class, args);\n\n        User user = run.getBean(\"haha\", User.class);\n        System.out.println(user);\n    }\n}<\/code><\/pre>\n<p>ps\uff1a\u5728 Spring Boot \u9879\u76ee\u91cc\uff0c<code>src\/main\/resources<\/code> \u4e0b\u9762\u7684\u6587\u4ef6\uff0c\u8fd0\u884c\u65f6\u4f1a\u8fdb\u5165 classpath\u3002<\/p>\n<h2><code>@ConfigurationProperties<\/code>\u3001<code>@Component<\/code>\u3001<code>@EnableConfigurationProperties<\/code>\u6ce8\u89e3\uff1a<\/h2>\n<p>@ConfigurationProperties:<\/p>\n<p>\u628a\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u4e00\u7ec4\u5c5e\u6027\uff0c\u7ed1\u5b9a\u5230\u4e00\u4e2a Java \u7c7b\u4e0a\u3002<\/p>\n<p>@Component:<\/p>\n<p>\u628a\u8fd9\u4e2a\u7c7b\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u3002<\/p>\n<p>@EnableConfigurationProperties:<\/p>\n<p>\u5f00\u542f\u67d0\u4e2a\u914d\u7f6e\u5c5e\u6027\u7c7b(\u5373\u4f7fConfigurationProperties\u53ef\u7528)\uff0c\u5e76\u628a\u5b83\u6ce8\u518c\u5230\u5bb9\u5668\u4e2d\u3002<\/p>\n<p>\u7531\u4e8e\u53ea\u6709\u5728\u5bb9\u5668\u4e2d\u7684\u7c7b\u624d\u6709<code>@ConfigurationProperties<\/code>\u529f\u80fd\uff0c\u6240\u4ee5\u8981\u4e48\u5148\u901a\u8fc7<code>@Component<\/code>\u5c06\u7c7b\u6ce8\u5165\u5230\u5bb9\u5668\u4e2d\uff0c\u8981\u4e48\u901a\u8fc7<\/p>\n<p><code>@EnableConfigurationProperties<\/code>\u89e3\u51b3\u3002<\/p>\n<p>\u4f8b\uff1a<\/p>\n<p>\u914d\u7f6e\u6587\u4ef6<code>application.properties<\/code>\u6709\uff1a<\/p>\n<pre><code>mycar.brand=BYD\nmycar.price=100000<\/code><\/pre>\n<p>\u7136\u540e\u518d\u5728Bean\u4e0b\u6709.java\u6587\u4ef6\uff1a<\/p>\n<pre><code>@Component\n@ConfigurationProperties(prefix = \"mycar\")\npublic class Car {\n\n    private String brand;\n    private Integer price;\n\n    public String getBrand() {\n        return brand;\n    }\n\n    public void setBrand(String brand) {\n        this.brand = brand;\n    }\n\n    public Integer getPrice() {\n        return price;\n    }\n\n    public void setPrice(Integer price) {\n        this.price = price;\n    }\n}<\/code><\/pre>\n<p>\u5c31\u4f1a\u81ea\u52a8\u628a\u6ce8\u518c\u5230\u5bb9\u5668\u91cc\u7684Car\u7c7b\u5bf9\u8c61\u7684brand\u548cprice\u8bbe\u7f6e\u6210\u5bf9\u5e94\u503c\u3002<\/p>\n<p>\u8fd9\u91cc\uff1a<\/p>\n<pre><code>@ConfigurationProperties(prefix = \"mycar\")<\/code><\/pre>\n<p>\u610f\u601d\u662f\uff1a\u8bfb\u53d6\u6240\u6709\u4ee5 <code>mycar<\/code> \u5f00\u5934\u7684\u914d\u7f6e\u3002<\/p>\n<p>\u6216\u8005\u5199\uff1a<\/p>\n<pre><code>@ConfigurationProperties(prefix = \"mycar\")\npublic class Car {\n\n    private String brand;\n    private Integer price;\n\n    \/\/ getter \/ setter\n}<\/code><\/pre>\n<p>\u7136\u540e\u5728\u542f\u52a8\u7c7b\u4e0a\u5199\uff1a<\/p>\n<pre><code>@SpringBootApplication\n@EnableConfigurationProperties(Car.class)\npublic class MainApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(MainApplication.class, args);\n    }\n}<\/code><\/pre>\n<p>\u4e5f\u53ef<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot2 \u5e95\u5c42\u6ce8\u89e3 @Configuration\u6ce8\u89e3\uff1a Spring\u4e2d\uff1a bean \u5305\u653e\u6570\u636e\u7c7b\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,3],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-java","category-3"],"_links":{"self":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":1,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":366,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/posts\/365\/revisions\/366"}],"wp:attachment":[{"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arknight.wiki\/index.php\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}