{"id":905,"date":"2023-03-11T16:52:02","date_gmt":"2023-03-11T08:52:02","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=905"},"modified":"2023-04-29T15:52:13","modified_gmt":"2023-04-29T07:52:13","slug":"spring-boot-obtains-bean-by-name-with-applicationcontext","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-boot-obtains-bean-by-name-with-applicationcontext\/","title":{"rendered":"Spring Boot\u901a\u8fc7\u540d\u79f0\u83b7\u53d6bean(applicationContext)"},"content":{"rendered":"<p>IOC\u5bb9\u5668\u6709<code>BeanFactory<\/code>\u548c<code>ApplicationContext<\/code>\u3002\u901a\u5e38\u5efa\u8bae\u4f7f\u7528\u540e\u8005\uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86\u524d\u8005\u7684\u529f\u80fd\u3002Spring\u7684\u6838\u5fc3\u662f<code>ApplicationContext<\/code>\uff0c\u5b83\u8d1f\u8d23\u7ba1\u7406 beans \u7684\u5b8c\u6574\u751f\u547d\u5468\u671f\u3002<\/p>\n<h3>ApplicationContextAware\u5b9e\u73b0\u7c7b<\/h3>\n<p><!-- more --><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4ece<code>ApplicationContext<\/code>\u91cc\u901a\u8fc7bean\u540d\u79f0\u83b7\u53d6\u5b89\u88c5\u7684bean\u8fdb\u884c\u67d0\u79cd\u64cd\u4f5c\u3002\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528<code>ApplicationContext<\/code>\uff0c\u800c\u9700\u8981\u501f\u52a9<code>ApplicationContextAware<\/code>\u3002\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\npublic class ApplicationContextHelper implements ApplicationContextAware {\n    private static ApplicationContext applicationContext;\n\n    public ApplicationContextHelper() {\n    }\n\n    \/**\n     * \u5b9e\u73b0ApplicationContextAware\u63a5\u53e3\u7684\u56de\u8c03\u65b9\u6cd5\uff0c\u8bbe\u7f6e\u4e0a\u4e0b\u6587\u73af\u5883\n     * @param applicationContext\n     * @throws BeansException\n     *\/\n    @Override\n    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {\n        ApplicationContextHelper.applicationContext = applicationContext;\n    }\n\n    \/**\n     * \u83b7\u5f97spring\u4e0a\u4e0b\u6587\n     * @return\n     *\/\n    public static ApplicationContext getApplicationContext() {\n        return applicationContext;\n    }\n\n    public static Object getBean(String beanName) {\n        return applicationContext != null ? applicationContext.getBean(beanName) : null;\n    }\n\n    public static ICustomService getCustomService(String beanName) {\n        return applicationContext != null ? (ICustomService) applicationContext.getBean(beanName) : null;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">public interface ICustomService {\n    void doSomeThing();\n}<\/code><\/pre>\n<p>\u5373\u58f0\u660e\u4e00\u4e2a<code>ApplicationContextHelper<\/code>\u7ec4\u4ef6\uff0c\u540d\u5b57\u968f\u610f\u3002\u5b83\u5b9e\u73b0\u4e86<code>ApplicationContextAware<\/code>\u63a5\u53e3\u3002\u5e76\u91cd\u5199<code>setApplicationContext<\/code>\u65b9\u6cd5\u3002\u5728\u8be5\u7ec4\u4ef6\u91cc\u53ef\u4ee5\u901a\u8fc7\u540d\u5b57\u83b7\u53d6\u67d0\u4e2abean\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<h3>Component\u7c7b\u83b7\u53d6<\/h3>\n<pre><code class=\"language-java\">@Slf4j\n@Component(value = &quot;a_service&quot;)\npublic class ACustomService implements ICustomService {\n    @Override\n    public File doSomeThing() {\n\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">ICustomService customService = ApplicationContextHelper.getBankService(String.format(&quot;%s_service&quot;, code).toLowerCase());\nif (customService == null) {\n    return new SettlementResult&lt;&gt;(RetCodeEnum.CUSTOM_SERVICE_INTERNAL_ERROR);\n}\ncustomService.doSomeThing();<\/code><\/pre>\n<h3>Bean\u65b9\u6cd5\u83b7\u53d6<\/h3>\n<pre><code class=\"language-java\">@SpringBootApplication\npublic class Application {\n\n   public static void main(String[] args) {\n      System.setProperty(&quot;user.timezone&quot;, &quot;Asia\/Shanghai&quot;);\n      TimeZone.setDefault(TimeZone.getTimeZone(&quot;Asia\/Shanghai&quot;));\n      SpringApplication.run(Application.class, args);\n   }\n\n   @Bean(name=&quot;restTemplate&quot;)\n   public RestTemplate restTemplate() {\n      RestTemplate restTemplate = new RestTemplate();\n      restTemplate.getMessageConverters()\n            .add(0, new StringHttpMessageConverter(Charsets.UTF_8));\n      return restTemplate;\n   }\n}\n\npublic class TestTask implements Callable&lt;String&gt; {\n\n    private RestTemplate restTemplate = (RestTemplate) ApplicationContextHelper.getBean( &quot;restTemplate&quot; );\n\n    public doSomeThing() {\n    }\n}<\/code><\/pre>\n<h3>\u901a\u8fc7Class\u83b7\u53d6<\/h3>\n<p>\u4e0d\u53ea\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5c5e\u4e8e\u67d0\u4e00\u7c7b<code>Class&lt;T&gt;<\/code>\u6765\u83b7\u53d6\u7c7b\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6240\u6709\u7684requestHandler\u53ca\u5176url\u6620\u5c04\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"language-java\">RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);<\/code><\/pre>\n<h3>\u53cd\u5c04\u6267\u884c\u65b9\u6cd5<\/h3>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u65b9\u5f0f\u6267\u884cBean\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u53cd\u5c04\u65b9\u5f0f\u6267\u884c\nObject object = ApplicationContextHelper.getBean(beanName);\n\nMethod method = ReflectionUtils.findMethod(object.getClass(), methodName, argsClass);\n\nObject o = ReflectionUtils.invokeMethod(method, object, args);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>IOC\u5bb9\u5668\u6709BeanFactory\u548cApplicationContext\u3002\u901a\u5e38\u5efa\u8bae\u4f7f\u7528\u540e\u8005\uff0c\u56e0\u4e3a\u5b83\u5305\u542b\u4e86\u524d\u8005\u7684 [&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-905","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\/905","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=905"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/905\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}