{"id":2030,"date":"2019-11-08T08:13:47","date_gmt":"2019-11-08T00:13:47","guid":{"rendered":"https:\/\/coderbee.net\/?p=2030"},"modified":"2019-11-07T21:26:56","modified_gmt":"2019-11-07T13:26:56","slug":"springboot-%e5%90%af%e5%8a%a8%e5%88%86%e6%9e%90%e5%85%ad-bean-%e7%9a%84%e5%88%9b%e5%bb%ba%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20191108\/2030","title":{"rendered":"SpringBoot \u542f\u52a8\u5206\u6790(\u516d) \u2014 Bean \u7684\u521b\u5efa\u8fc7\u7a0b"},"content":{"rendered":"<h1>1. Bean \u7684\u521b\u5efa\u8fc7\u7a0b<\/h1>\n<p>\u4e00\u4e2a bean \u7684\u521b\u5efa\u8fc7\u7a0b\u6211\u60f3\u7528\u4e0b\u9762\u7684\u56fe\u6765\u8868\u793a\uff1a<br \/>\n<a href=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/11\/spring-create-bean.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/11\/spring-create-bean.png\" alt=\"\" width=\"1793\" height=\"853\" class=\"alignnone size-full wp-image-2031\" \/><\/a><\/p>\n<h1>2. bean \u521b\u5efa\u76f8\u5173\u7684\u6838\u5fc3\u6269\u5c55\u673a\u5236<\/h1>\n<p><!--more--><\/p>\n<h2>2.1 InstantiationAwareBeanPostProcessor<\/h2>\n<p>BeanPostProcessor \u7684\u5b50\u63a5\u53e3\uff0c\u6dfb\u52a0\u4e86 \u5b9e\u4f8b\u5316\u524d\u7684\u56de\u8c03\uff0c\u5b9e\u4f8b\u5316\u540e\u3001\u4f46\u663e\u5f0f\u8bbe\u7f6e\u5c5e\u6027\u6216\u81ea\u52a8\u6ce8\u5165\u524d\u7684\u56de\u8c03\u3002<\/p>\n<p>\u901a\u5e38\u7528\u4e8e\u6291\u5236\u9ed8\u8ba4\u7684\u5bf9\u7279\u5b9a\u76ee\u6807 bean \u7684\u521d\u59cb\u5316\uff0c\u4f8b\u5982\u7ed9\u7279\u5b9a\u7684 TargetSources\uff08\u6c60\u5316\u3001\u5ef6\u8fdf\u521d\u59cb\u5316\u7684\u76ee\u6807\u7b49\uff09\u521b\u5efa\u4ee3\u7406\uff0c\u6216\u8005\u7528\u4e8e\u5b9e\u73b0\u989d\u5916\u7684\u6ce8\u5165\u7b56\u7565\u3001\u4f8b\u5982\u5b57\u6bb5\u6ce8\u5165\u3002<br \/>\n\uff08AnnotationAwareAspectJAutoProxyCreator \u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u907f\u514d\u7ed9\u7279\u5b9a\u7684 bean \u521b\u5efa\u4ee3\u7406\uff1bAutowiredAnnotationBeanPostProcessor \u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7528\u4e8e\u5b9e\u73b0 @Autowired\/@Value \u7684\u5b57\u6bb5\/\u65b9\u6cd5 \u6ce8\u5165\uff09<\/p>\n<p>\u6ce8\u610f\uff1a\u8fd9\u4e2a\u63a5\u53e3\u662f\u4e2a\u7279\u6b8a\u76ee\u7684\u7684\u63a5\u53e3\uff0c\u4e3b\u8981\u7528\u4e8e\u6846\u67b6\u5185\u90e8\u3002\u5efa\u8bae\u5c3d\u53ef\u80fd\u91c7\u7528\u5b9e\u73b0\u666e\u901a\u7684 BeanPostProcessor \u63a5\u53e3\uff0c\u6216\u8005\u7ee7\u627f\u81ea InstantiationAwareBeanPostProcessorAdapter \u4ee5\u7ed5\u5f00\u5bf9\u8fd9\u4e2a\u63a5\u53e3\u7684\u6269\u5c55\u3002<\/p>\n<pre><code class=\"java\">public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {\n\n  \/**\n   * \u5728 bean \u88ab\u5b9e\u4f8b\u5316\u4e4b\u524d\u5e94\u7528\u3002\u8fd4\u56de\u7684\u5bf9\u8c61\u53ef\u80fd\u662f\u4e2a\u4ee3\u7406\u800c\u4e0d\u662f\u76ee\u6807 bean\uff0c\u6709\u6548 \u9690\u7792\u4e86\u76ee\u6807 bean \u7684\u9ed8\u8ba4\u521b\u5efa\u3002\n   * \u5982\u679c\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u975e null \u5bf9\u8c61\uff0cbean\u7684\u540e\u7eed\u521b\u5efa\u8fc7\u7a0b\u5c06\u88ab\u77ed\u8def\uff0c\u53ea\u6709 BeanPostProcessor.postProcessAfterInitialization \u88ab\u5e94\u7528\u3002\n   *\/\n  Object postProcessBeforeInstantiation(Class&lt;?&gt; beanClass, String beanName) throws BeansException;\n\n  \/**\n   * \u5728 bean \u88ab\u5b9e\u4f8b\u5316\uff08\u901a\u8fc7 \u6784\u9020\u5668\u3001\u5de5\u5382\u65b9\u6cd5\uff09\u540e\u3001Spring \u8bbe\u7f6e\u5c5e\u6027\u4e4b\u524d\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002\n   * \u8fd9\u662f\u5728 Spring \u81ea\u52a8\u6ce8\u5165\u63a5\u5165\u4e4b\u524d\u5bf9\u7ed9\u5b9a bean \u5b9e\u4f8b\u6267\u884c\u5b9a\u5236\u7684\u5b57\u6bb5\u6ce8\u5165\u7684\u7406\u60f3\u56de\u8c03\u94a9\u5b50\u3002\n   *\/\n  boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;\n\n  \/**\n   * \u540e\u7f6e\u5904\u7406\u7ed9\u5b9a\u7684\u5c5e\u6027\u503c\uff0c\u5728 bean \u5de5\u5382\u628a\u5b83\u4eec\u5e94\u7528\u5230\u7ed9\u5b9a bean \u4e4b\u524d\u3002\u5141\u8bb8\u68c0\u67e5\u6240\u6709\u7684\u4f9d\u8d56\u662f\u5426\u90fd\u6ee1\u8db3\u4e86\u3002\n   * \u540c\u65f6\u4e5f\u5141\u8bb8\u66ff\u6362\u8981\u5e94\u7528\u7684\u5c5e\u6027\u503c\uff0c\u901a\u5e38\u662f\u901a\u8fc7\u57fa\u4e8e\u539f\u59cb PropertyValues \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 MutablePropertyValues \u5b9e\u4f8b\uff0c\u6dfb\u52a0\u6216\u79fb\u9664\u7279\u5b9a\u7684\u503c\u3002\n   *\/\n  PropertyValues postProcessPropertyValues(\n      PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException;\n\n}\n<\/code><\/pre>\n<h2>2.2 SmartInstantiationAwareBeanPostProcessor<\/h2>\n<p>InstantiationAwareBeanPostProcessor \u63a5\u53e3\u7684\u6269\u5c55\uff0c\u6dfb\u52a0\u4e86\u4e00\u4e2a\u56de\u8c03\u7528\u4e8e\u9884\u6d4b\u5904\u7406\u7684 Bean \u7684\u6700\u7ec8\u7c7b\u578b\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u8fd9\u662f\u4e2a\u7279\u6b8a\u76ee\u7684\u7684\u63a5\u53e3\uff0c\u4e3b\u8981\u7528\u4e8e\u6846\u67b6\u5185\u90e8\u3002\u901a\u5e38\uff0c\u5e94\u7528\u63d0\u4f9b\u7684\u540e\u7f6e\u5904\u7406\u5668\u5e94\u5f53\u7b80\u5355\u5b9e\u73b0\u666e\u901a\u7684 BeanPostProcessor \u63a5\u53e3\u6216\u7ee7\u627f\u81ea InstantiationAwareBeanPostProcessorAdapter \u7c7b\u3002\u65b0\u7684\u65b9\u6cd5\u53ef\u80fd\u6dfb\u52a0\u5230\u8fd9\u4e2a\u63a5\u53e3\u3002<\/p>\n<pre><code class=\"java\">public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {\n  \/**\n   * \u9884\u6d4b\u7ed9\u5b9a beanName \u7684\u6700\u7ec8 Bean \u7c7b\u578b\n   *\/\n  Class&lt;?&gt; predictBeanType(Class&lt;?&gt; beanClass, String beanName) throws BeansException;\n\n  \/**\n   * \u786e\u5b9a\u7ed9\u5b9a bean \u7684\u5019\u9009\u6784\u9020\u5668\n   *\/\n  Constructor&lt;?&gt;[] determineCandidateConstructors(Class&lt;?&gt; beanClass, String beanName) throws BeansException;\n\n  Object getEarlyBeanReference(Object bean, String beanName) throws BeansException;\n}\n<\/code><\/pre>\n<h2>2.3 MergedBeanDefinitionPostProcessor<\/h2>\n<p>merged bean definition\uff0c\u5408\u5e76\u7684 bean \u5b9a\u4e49\uff0c\u662f\u539f\u59cb bean \u5b9a\u4e49\u7684\u4e00\u4e2a\u62f7\u8d1d\uff0cSpring BeanFacotry \u7528\u6765\u521b\u5efa bean \u5b9e\u4f8b\u3002<\/p>\n<p>\u8be5\u63a5\u53e3\u662f\u7528\u4e8e \u5408\u5e76\u7684 bean \u5b9a\u4e49\u7684 \u540e\u7f6e\u5904\u7406\u5668\u63a5\u53e3\u3002BeanPostProcessor \u5b9e\u73b0\u53ef\u80fd\u5b9e\u73b0\u8fd9\u4e2a\u5b50\u63a5\u53e3\u4ee5\u4fbf \u540e\u7f6e\u5904\u7406 \u5408\u5e76\u7684 bean \u5b9a\u4e49\u3002<\/p>\n<p>postProcessMergedBeanDefinition \u65b9\u6cd5\u53ef \u5185\u7701(introspect) bean \u5b9a\u4e49\u4ee5\u4fbf\u5728\u540e\u7f6e\u5904\u7406\u5b9e\u9645\u7684 bean \u5b9e\u4f8b\u4e4b\u524d\u51c6\u5907\u4e00\u4e9b\u5143\u6570\u636e\u7f13\u5b58\u3002<\/p>\n<p>\u521b\u5efa\u4ee3\u7406\uff1bAutowiredAnnotationBeanPostProcessor \u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7528\u4e8e\u6536\u96c6\u5e26\u6709 @Autowired\/@Value \u6ce8\u89e3\u7684\u5b57\u7b26\u3001\u65b9\u6cd5\uff0c\u5c01\u88c5\u6210\u6ce8\u5165\u5143\u6570\u636e InjectionMetadata \u3002<\/p>\n<pre><code class=\"java\">public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {\n  void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class&lt;?&gt; beanType, String beanName);\n}\n<\/code><\/pre>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Bean \u7684\u521b\u5efa\u8fc7\u7a0b \u4e00\u4e2a bean \u7684\u521b\u5efa\u8fc7\u7a0b\u6211\u60f3\u7528\u4e0b\u9762\u7684\u56fe\u6765\u8868\u793a\uff1a 2 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20191108\/2030\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[172],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2030"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2030"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2030\/revisions"}],"predecessor-version":[{"id":2034,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2030\/revisions\/2034"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}