{"id":7601,"date":"2023-11-28T19:42:31","date_gmt":"2023-11-28T11:42:31","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7601"},"modified":"2023-12-05T15:40:37","modified_gmt":"2023-12-05T07:40:37","slug":"springboot-zhong-shen-me-shi-javaconfig","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-shen-me-shi-javaconfig\/","title":{"rendered":"SpringBoot \u4e2d\u4ec0\u4e48\u662f JavaConfig?"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0cJavaConfig \u662f\u6307\u4f7f\u7528 Java \u7c7b\u6765\u8fdb\u884c Spring \u914d\u7f6e\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u76f8\u6bd4\u4e8e\u4f20\u7edf\u7684 XML \u914d\u7f6e\uff0cJavaConfig \u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u4ee3\u7801\u7684\u914d\u7f6e\u65b9\u5f0f\uff0c\u901a\u8fc7 Java \u7c7b\u6765\u5b9a\u4e49\u548c\u914d\u7f6e Spring Bean\u3001\u7ec4\u4ef6\u626b\u63cf\u3001\u4f9d\u8d56\u6ce8\u5165\u7b49\u3002<\/p>\n<p>\u5728 Spring Boot \u4e2d\uff0cJavaConfig \u4e3b\u8981\u901a\u8fc7 <code>@Configuration<\/code> \u6ce8\u89e3\u548c\u76f8\u5173\u7684\u6ce8\u89e3\uff08\u5982 <code>@Bean<\/code>\u3001<code>@ComponentScan<\/code>\uff09\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b JavaConfig \u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<strong><code>@Configuration<\/code> \u6ce8\u89e3\uff1a<\/strong> \u4f7f\u7528 <code>@Configuration<\/code> \u6ce8\u89e3\u6807\u6ce8\u4e00\u4e2a\u7c7b\uff0c\u8868\u793a\u8fd9\u4e2a\u7c7b\u662f\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5b83\u53ef\u4ee5\u5305\u542b bean \u7684\u5b9a\u4e49\u3001\u7ec4\u4ef6\u626b\u63cf\u7b49\u914d\u7f6e\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Configuration;\n\n@Configuration\npublic class MyConfig {\n    \/\/ Configuration details...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong><code>@Bean<\/code> \u6ce8\u89e3\uff1a<\/strong> \u4f7f\u7528 <code>@Bean<\/code> \u6ce8\u89e3\u6807\u6ce8\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8868\u793a\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a bean \u5b9e\u4f8b\u3002Spring Boot \u5c06\u81ea\u52a8\u5c06\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\u6ce8\u518c\u4e3a Spring \u5bb9\u5668\u4e2d\u7684\u4e00\u4e2a bean\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Bean;\n\n@Configuration\npublic class MyConfig {\n\n    @Bean\n    public MyBean myBean() {\n        return new MyBean();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong><code>@ComponentScan<\/code> \u6ce8\u89e3\uff1a<\/strong> \u4f7f\u7528 <code>@ComponentScan<\/code> \u6ce8\u89e3\u6807\u6ce8\u4e00\u4e2a\u7c7b\uff0c\u8868\u793a\u542f\u7528\u7ec4\u4ef6\u626b\u63cf\uff0c\u81ea\u52a8\u626b\u63cf\u5e76\u6ce8\u518c\u6807\u6709 <code>@Component<\/code> \u6ce8\u89e3\u7684\u7c7b\u4e3a Spring Bean\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.ComponentScan;\n\n@Configuration\n@ComponentScan(basePackages = &quot;com.example&quot;)\npublic class MyConfig {\n    \/\/ Configuration details...\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7 JavaConfig\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u907f\u514d\u4f7f\u7528 XML \u8fdb\u884c\u914d\u7f6e\uff0c\u800c\u662f\u901a\u8fc7\u4ee3\u7801\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u548c\u914d\u7f6e Spring Bean\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3001\u7ef4\u62a4\u6027\uff0c\u5e76\u4e14\u66f4\u7b26\u5408 Java \u7684\u8bed\u8a00\u7279\u6027\u3002\u5728 Spring Boot \u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 JavaConfig \u4f5c\u4e3a\u4e3b\u8981\u7684\u914d\u7f6e\u65b9\u5f0f\uff0c\u642d\u914d <code>@SpringBootApplication<\/code> \u6ce8\u89e3\uff0c\u4ee5\u4fbf\u66f4\u65b9\u4fbf\u5730\u5b9e\u73b0\u5feb\u901f\u5f00\u53d1\u548c\u90e8\u7f72\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0cJavaConfig \u662f\u6307\u4f7f\u7528 Java \u7c7b\u6765\u8fdb\u884c Spring \u914d\u7f6e\u7684\u4e00\u79cd\u65b9 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-7601","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7601"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7601\/revisions"}],"predecessor-version":[{"id":16529,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7601\/revisions\/16529"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7601"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}