{"id":461,"date":"2024-12-16T19:50:58","date_gmt":"2024-12-16T11:50:58","guid":{"rendered":"https:\/\/www.huangkeqin.cn\/?p=461"},"modified":"2024-12-27T12:39:14","modified_gmt":"2024-12-27T04:39:14","slug":"%e5%9c%a8springboot%e4%b8%ad%e5%ae%9e%e4%bd%93%e7%b1%bb%e6%98%af%e4%b8%8d%e6%98%af%e9%83%bd%e8%a6%81%e5%8a%a0%e4%b8%8acomponent%e6%b3%a8%e8%a7%a3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.huangkeqin.cn\/index.php\/2024\/12\/16\/%e5%9c%a8springboot%e4%b8%ad%e5%ae%9e%e4%bd%93%e7%b1%bb%e6%98%af%e4%b8%8d%e6%98%af%e9%83%bd%e8%a6%81%e5%8a%a0%e4%b8%8acomponent%e6%b3%a8%e8%a7%a3%ef%bc%9f\/","title":{"rendered":"\u5728springboot\u4e2d\u5b9e\u4f53\u7c7b\u662f\u4e0d\u662f\u90fd\u8981\u52a0\u4e0acomponent\u6ce8\u89e3\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 <strong>Spring Boot<\/strong> \u4e2d\uff0c\u5b9e\u4f53\u7c7b <strong>\u5e76\u4e0d\u9700\u8981<\/strong> \u4e00\u5b9a\u8981\u52a0\u4e0a <code>@Component<\/code> \u6ce8\u89e3\u3002\u662f\u5426\u9700\u8981\u52a0 <code>@Component<\/code> \u53d6\u51b3\u4e8e\u5b9e\u4f53\u7c7b\u7684\u7528\u9014\u548c\u662f\u5426\u9700\u8981\u88ab Spring \u7ba1\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u52a0 <code>@Component<\/code>\uff1f<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9700\u8981 Spring \u7ba1\u7406\u548c\u6ce8\u5165\u7684\u7c7b<\/strong>\uff1a \u5982\u679c\u4f60\u7684\u5b9e\u4f53\u7c7b\u9700\u8981\u88ab Spring \u7ba1\u7406\uff0c\u4f8b\u5982\u901a\u8fc7 <code>@Autowired<\/code> \u6ce8\u5165\u5230\u5176\u4ed6\u7c7b\u4e2d\u4f7f\u7528\uff0c\u5c31\u9700\u8981\u4f7f\u7528 <code>@Component<\/code> \u6216\u5176\u884d\u751f\u6ce8\u89e3\uff08\u5982 <code>@Service<\/code>\u3001<code>@Repository<\/code> \u7b49\uff09\u3002<strong>\u4f8b\u5982\uff1a<\/strong><code>@Component public class SomeBean { \/\/ Spring \u4f1a\u7ba1\u7406\u8fd9\u4e2a\u7c7b\u7684\u751f\u547d\u5468\u671f }<\/code><\/li>\n\n\n\n<li><strong>\u52a8\u6001\u4ee3\u7406\u3001AOP\u7b49\u573a\u666f<\/strong>\uff1a \u5982\u679c\u4f60\u7684\u5b9e\u4f53\u7c7b\u9700\u8981\u5728 Spring \u5bb9\u5668\u4e2d\u88ab\u4ee3\u7406\uff08\u6bd4\u5982 AOP \u5207\u9762\u529f\u80fd\uff09\uff0c\u5c31\u9700\u8981\u5c06\u5176\u4ea4\u7ed9 Spring \u7ba1\u7406\uff0c\u4f7f\u7528 <code>@Component<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u4ec0\u4e48\u65f6\u5019\u4e0d\u9700\u8981\u52a0 <code>@Component<\/code>\uff1f<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u666e\u901a\u7684\u5b9e\u4f53\u7c7b\uff08POJO\uff09<\/strong>\uff1a \u5982\u679c\u4f60\u7684\u7c7b\u53ea\u662f\u666e\u901a\u7684\u5b9e\u4f53\u7c7b\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u7684\u8868\u6620\u5c04\u7c7b\uff08\u901a\u5e38\u4f7f\u7528 JPA\uff09\uff0c\u4e00\u822c\u4e0d\u9700\u8981\u52a0 <code>@Component<\/code>\uff0c\u8fd9\u4e9b\u7c7b\u53ea\u662f\u6570\u636e\u8f7d\u4f53\uff0c\u4e0d\u9700\u8981\u7531 Spring \u5bb9\u5668\u7ba1\u7406\u3002<strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Data\n@AllArgsConstructor\n@NoArgsConstructor\npublic class User {\n    private Long id;\n    private String name;\n    private String email;\n}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e9b\u7c7b\u901a\u5e38\u4f1a\u901a\u8fc7 ORM \u6846\u67b6\uff08\u5982 Hibernate\/JPA\uff09\u521b\u5efa\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u7531 Spring \u5bb9\u5668\u7ba1\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e34\u65f6\u521b\u5efa\u7684\u5bf9\u8c61<\/strong>\uff1a \u5982\u679c\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u662f\u4e34\u65f6\u521b\u5efa\u7684\uff0c\u6216\u8005\u76f4\u63a5\u901a\u8fc7 <code>new<\/code> \u5173\u952e\u5b57\u521b\u5efa\uff0c\u5c31\u4e0d\u9700\u8981\u52a0 <code>@Component<\/code>\u3002<strong>\u4f8b\u5982\uff1a<\/strong><code>public class Total { private Integer signedInCount; private Integer notSignedInCount; \/\/ \u4f7f\u7528\u65f6\u76f4\u63a5 new Total(signedInCount, notSignedInCount) }<\/code><\/li>\n\n\n\n<li><strong>\u4e0d\u9700\u8981\u88ab\u6ce8\u5165\u6216\u7ba1\u7406\u7684\u7c7b<\/strong>\uff1a \u5982\u679c\u67d0\u4e2a\u7c7b\u7684\u4f5c\u7528\u53ea\u662f\u5728\u5185\u90e8\u903b\u8f91\u4e2d\u4f7f\u7528\uff0c\u4e14\u4e0d\u9700\u8981\u4f9d\u8d56\u6ce8\u5165\uff0c\u4e5f\u4e0d\u9700\u8981\u5728 Spring \u5bb9\u5668\u4e2d\u6ce8\u518c\uff0c\u5219\u4e0d\u9700\u8981 <code>@Component<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5e38\u89c1\u9519\u8bef\u573a\u666f<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u666e\u901a POJO \u7c7b\u8bef\u52a0 <code>@Component<\/code><\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u7ed9\u666e\u901a\u7684\u5b9e\u4f53\u7c7b\u52a0\u4e0a\u4e86 <code>@Component<\/code>\uff0cSpring \u4f1a\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a Bean \u7ba1\u7406\uff0c\u4f46\u7531\u4e8e\u5176\u5e76\u6ca1\u6709\u5b9e\u9645\u7684\u4e1a\u52a1\u903b\u8f91\u6216\u6ce8\u5165\u9700\u6c42\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65e0\u610f\u4e49\u7684 Bean \u6ce8\u518c\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>@Component<\/code> \u6ce8\u89e3\u4e0e\u624b\u52a8\u521b\u5efa\u5bf9\u8c61\u51b2\u7a81<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5728\u4ee3\u7801\u4e2d\u65e2\u7528 <code>@Component<\/code> \u6ce8\u89e3\u8ba9 Spring \u7ba1\u7406 Bean\uff0c\u540c\u65f6\u53c8\u5728\u4ee3\u7801\u4e2d\u624b\u52a8 <code>new<\/code>\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u884c\u4e3a\u4e0d\u53ef\u9884\u6d4b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u666e\u901a\u5b9e\u4f53\u7c7b\uff08POJO\uff09<\/strong>\uff1a\u4e0d\u9700\u8981 <code>@Component<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u9700\u8981 Spring \u7ba1\u7406\u7684\u7c7b<\/strong>\uff1a\u5982\u4e1a\u52a1\u903b\u8f91\u7c7b\u3001\u5de5\u5177\u7c7b\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>@Component<\/code> \u6216\u5176\u6d3e\u751f\u6ce8\u89e3\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5e93\u6620\u5c04\u7c7b\uff08\u5982 JPA \u5b9e\u4f53\u7c7b\uff09<\/strong>\uff1a\u65e0\u9700 <code>@Component<\/code>\uff0c\u7531 JPA \u6216\u5176\u4ed6\u6846\u67b6\u7ba1\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u52a0\u4e0d\u52a0 <code>@Component<\/code> \u7684\u6838\u5fc3\u5224\u65ad\u662f\uff1a<strong>\u8fd9\u4e2a\u7c7b\u662f\u5426\u9700\u8981\u88ab Spring \u5bb9\u5668\u7ba1\u7406<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u5b9e\u4f53\u7c7b \u5e76\u4e0d\u9700\u8981 \u4e00\u5b9a\u8981\u52a0\u4e0a @Component \u6ce8\u89e3\u3002\u662f\u5426\u9700\u8981\u52a0 @Com [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3],"tags":[],"class_list":["post-461","post","type-post","status-publish","format-standard","hentry","category-java","category-3"],"_links":{"self":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts\/461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/comments?post=461"}],"version-history":[{"count":2,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":550,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/posts\/461\/revisions\/550"}],"wp:attachment":[{"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/media?parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/categories?post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.huangkeqin.cn\/index.php\/wp-json\/wp\/v2\/tags?post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}