{"id":107,"date":"2023-06-24T09:37:20","date_gmt":"2023-06-24T01:37:20","guid":{"rendered":"https:\/\/www.jishuge.cn\/?p=107"},"modified":"2023-06-24T09:37:20","modified_gmt":"2023-06-24T01:37:20","slug":"springboot%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%99%ae%e9%80%9a%e7%b1%bb%e4%bd%bf%e7%94%a8bean%e3%80%82","status":"publish","type":"post","link":"https:\/\/blog.jishuge.cn\/?p=107","title":{"rendered":"SpringBoot\u9879\u76ee\u4e2d\u666e\u901a\u7c7b\u4f7f\u7528Bean\u3002"},"content":{"rendered":"\n<p>\u6211\u4eec\u5728SpringBoot\u4e2d\u4f7f\u7528Bean\u7684\u65b9\u5f0f\u662f<code>@Autowired<\/code> \u65b9\u5f0f\u81ea\u52a8\u6ce8\u5165\u7684\uff0c\u4f46\u662f\u5728\u67d0\u4e9b\u666e\u901a\u7c7b\u4e2d\u4f7f\u7528Bean\u7684\u8bdd\uff0c\u5c31\u4e0d\u80fd\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6bd4\u5982\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n\n\n\n<p>\u5728\u4f7f\u7528  <code>import org.java_websocket.client.WebSocketClient;<\/code> \u4f5c\u4e3a\u5ba2\u6237\u7aef\u53bb\u94fe\u63a5WebSocket\u670d\u52a1\u5668\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u4f7f\u7528\u6709\u53c2\u6784\u9020\uff0c\u800c\u4e0d\u80fd\u81ea\u52a8\u6ce8\u5165\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8981\u5728WebSocketClient\u4e2d\u4f7f\u7528\u81ea\u52a8\u6ce8\u5165\u8fc7\u7684redis\u3001mysql\u7b49\uff0c\u4f7f\u7528 <code>@Autowired<\/code> \u83b7\u53d6\u7684\u5bf9\u8c61\u662fnull\uff0c\u90a3\u4e48\u53ea\u80fd\u76f4\u63a5\u4eceSpringBoot\u7684Bean\u5bf9\u8c61\u7ba1\u7406\u5668\u4e2d\u62ff\u53d6\u6240\u9700\u8981\u7684Bean\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u64cd\u4f5c\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5b9a\u4e49\u4e00\u4e2a\u83b7\u53d6Bean\u7684\u5de5\u5177\u7c7b\u3002<\/h2>\n\n\n\n<p>\u5b9a\u4e49\u5de5\u5177\u7c7b SpringUtil \uff08\u540d\u5b57\u81ea\u5b9a\u4e49\uff09 \u5b9e\u73b0 <code> org.springframework.context.ApplicationContextAware<\/code> \u8fd9\u4e2a\u63a5\u53e3\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/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=\"\">@Component\npublic class SpringUtil implements ApplicationContextAware {\n\n    private static ApplicationContext applicationContext;\n\n    @Override\n    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {\n        SpringUtil.applicationContext = applicationContext;\n    }\n\n    public static &lt;T> T getBean(Class&lt;T> beanClass) {\n        return applicationContext.getBean(beanClass);\n    }\n\n    public static &lt;T> T getBean(String beanName, Class&lt;T> beanClass) {\n        return applicationContext.getBean(beanName, beanClass);\n    }\n}<\/pre>\n\n\n\n<p>\u8bb0\u4f4f\uff0c\u8fd9\u4e2a\u7c7b\u4e00\u5b9a\u8981\u6dfb\u52a0 <code><strong>@Component <\/strong><\/code>\u7ec4\u4ef6\u6ce8\u89e3<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u4f7f\u7528demo<\/h2>\n\n\n\n<p>\u5728\u4e00\u4e2a\u666e\u901a\u7c7b\u4e2d\u4f7f\u7528\uff0c\u6bd4\u5982\u6211\u4eec\u60f3\u83b7\u53d6RabbitMq\u7684\u81ea\u52a8\u6ce8\u5165\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=\"\">RabbitTemplate rabbitTemplate= SpringUtil.getBean(RabbitTemplate.class);\nrabbitTemplate.convertAndSend(\"test_message\",\"test\"+new Date());<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u539f\u7406\uff1a\u5c31\u662fSpringBoot\u7684Bean\u90fd\u4ea4\u7531ApplicationContext applicationContext \u6765\u7ba1\u7406\uff0c\u6211\u4eec\u53ea\u662f\u95f4\u63a5\u7684\u4ece ApplicationContext applicationContext \u8fd9\u4e2a\u5bf9\u8c61\u4e2d\u83b7\u53d6\uff0c\u66f4\u591a\u7684\u53ef\u4ee5\u53c2\u8003Spring\u7684\u539f\u7406\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5728SpringBoot\u4e2d\u4f7f\u7528Bean\u7684\u65b9\u5f0f\u662f@Autowired \u65b9\u5f0f\u81ea\u52a8\u6ce8\u5165\u7684\uff0c\u4f46\u662f\u5728\u67d0\u4e9b\u666e\u901a\u7c7b\u4e2d\u4f7f\u7528Bean\u7684\u8bdd\uff0c\u5c31\u4e0d\u80fd\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u7528\u5176\u4ed6\u65b9\u5f0f\u83b7\u53d6Bean<\/p>\n","protected":false},"author":1,"featured_media":109,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=107"}],"version-history":[{"count":1,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions\/110"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/media\/109"}],"wp:attachment":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}