{"id":923,"date":"2023-03-11T17:17:30","date_gmt":"2023-03-11T09:17:30","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=923"},"modified":"2023-04-29T15:47:51","modified_gmt":"2023-04-29T07:47:51","slug":"spring-boot-manually-injects-beans","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-boot-manually-injects-beans\/","title":{"rendered":"Spring Boot\u624b\u52a8\u6ce8\u5165Bean"},"content":{"rendered":"<h2>\u5e94\u7528\u573a\u666f<\/h2>\n<p>Spring Boot\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9047\u5230<code>@Autowired<\/code>\u6ce8\u5165\u5bf9\u8c61\u4e3anull\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u7684\u539f\u56e0\u5305\u542b\uff1a<\/p>\n<p>\uff081\uff09\u5f53\u524d\u7c7b\u7684\u521d\u59cb\u5316\u5728\u6ce8\u5165\u7c7b\u6ce8\u5165\u4e4b\u524d<br \/>\n\uff082\uff09\u591a\u7ebf\u7a0b\u73af\u5883\uff0c\u65b0\u521b\u5efa\u7684\u7ebf\u7a0b\u4e2d\uff0c\u662f\u4e0d\u80fd\u81ea\u52a8\u6ce8\u5165<code>Bean\/Service<\/code>\u7684<\/p>\n<p><!-- more --><\/p>\n<p>\u6b64\u65f6\u5c31\u9700\u8981\u624b\u52a8\u6ce8\u5165Bean<\/p>\n<h2>\u624b\u52a8\u83b7\u53d6Bean\u7684\u5de5\u5177\u7c7b<\/h2>\n<p>\u539f\u7406\uff0c\u5728\u9879\u76ee\u542f\u52a8\u65f6\u83b7\u53d6\u5230Spring Boot\u4e0a\u4e0b\u6587\uff0c\u4ece\u4e0a\u4e0b\u6587\u4e2d\u83b7\u53d6\u5230Bean<\/p>\n<pre><code class=\"language-java\">\/**\n * \u63a5\u901a\u8fc7Spring \u4e0a\u4e0b\u6587\u83b7\u53d6SpringBean\n * @author yezhou\n * @version 1.0\n * @date 2019\/8\/15 18:39\n *\/\n@Component\npublic class ApplicationContextUtil implements ApplicationContextAware {\n    \/\/ Spring Boot\u5e94\u7528\u4e0a\u4e0b\u6587\u73af\u5883\n    private static ApplicationContext applicationContext;\n\n    \/**\n     * \u5b9e\u73b0ApplicationContextAware\u63a5\u53e3\u7684\u56de\u8c03\u65b9\u6cd5\uff0c\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u73af\u5883\n     *\/\n    @Override\n    public void setApplicationContext(ApplicationContext applicationContext)\n            throws BeansException {\n        ApplicationContextUtil.applicationContext = applicationContext;\n    }\n\n    public static ApplicationContext getApplicationContext() {\n        return applicationContext;\n    }\n\n    \/**\n     * \u83b7\u53d6\u5bf9\u8c61\uff1a\u8fd9\u91cc\u91cd\u5199getBean\u65b9\u6cd5\uff0c\u8d77\u4e3b\u8981\u4f5c\u7528\n     * example: getBean(&quot;userService&quot;)  \/\/\u6ce8\u610f\uff1a \u7c7b\u540d\u9996\u5b57\u6bcd\u4e00\u5b9a\u8981\u5c0f\u5199\uff01\n     *\/\n    public static Object getBean(String beanId) throws BeansException {\n        return applicationContext.getBean(beanId);\n    }\n}<\/code><\/pre>\n<h2>\u5728\u8d85\u524d\u521d\u59cb\u5316\u7c7b\u6216\u7ebf\u7a0b\u7c7b\u4e2d\u624b\u52a8\u83b7\u53d6Bean\/Service<\/h2>\n<h3>\u8d85\u524d\u521d\u59cb\u5316\u7c7b<\/h3>\n<pre><code class=\"language-java\">@Component(value = &quot;UserAuthDetailsService&quot;)\npublic class UserAuthDetailsService implements UserDetailsService {\n<\/code><\/pre>\n<pre><code class=\"language-java\">public class SMSCodeTokenGranter extends AbstractTokenGranter {\n    private static final String GRANT_TYPE = &quot;sms_code&quot;;\n\n    private UserAuthDetailsService authUserDetailsService;\n\n    public SMSCodeTokenGranter(AuthorizationServerTokenServices tokenServices,\n                                  ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {\n        super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);\n    }\n\n    @Override\n    protected OAuth2Authentication getOAuth2Authentication(ClientDetails client,\n                                                           TokenRequest tokenRequest) {\n        Map&lt;String, String&gt; parameters = new LinkedHashMap&lt;String, String&gt;(tokenRequest.getRequestParameters());\n        String userMobileNo = parameters.get(&quot;username&quot;);  \/\/\u5ba2\u6237\u7aef\u63d0\u4ea4\u7684\u7528\u6237\u540d\n        String smsCode = parameters.get(&quot;sms_code&quot;);  \/\/\u5ba2\u6237\u7aef\u63d0\u4ea4\u7684\u9a8c\u8bc1\u7801\n\n        authUserDetailsService = (UserAuthDetailsService) ApplicationContextUtil.getBean(&quot;UserAuthDetailsService&quot;);\n        UserDetails user = authUserDetailsService.loadUserByUsername(userMobileNo);<\/code><\/pre>\n<h3>\u7ebf\u7a0b\u7c7b<\/h3>\n<pre><code class=\"language-java\">public class SdReleaseReview implements Runnable {\n\u00a0 \u00a0 private CalendarService calendarService;\n\n\u00a0 \u00a0 public SdReleaseReview() {\n\u00a0 \u00a0 \u00a0 \u00a0 this.calendarService=(CalendarService)SpringContextUtil.getBean(&quot;calendarService&quot;);\n\u00a0 \u00a0 }\n\n\u00a0 \u00a0 @Override\n\u00a0 \u00a0 public void run() {\n\u00a0 \u00a0 \u00a0 \u00a0 try {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ...\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FactoryCalendar factoryCalendar = calendarService.getByExistDate(&quot;2000&quot;, today);\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ...\n\u00a0 \u00a0 \u00a0 \u00a0 } catch (Exception e) {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ...\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5e94\u7528\u573a\u666f Spring Boot\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9047\u5230@Autowired\u6ce8\u5165\u5bf9\u8c61\u4e3anull\u7684\u95ee\u9898\uff0c\u53ef\u80fd\u7684\u539f\u56e0\u5305 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-923","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=923"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/923\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}