{"id":1287,"date":"2015-08-21T08:57:04","date_gmt":"2015-08-21T00:57:04","guid":{"rendered":"http:\/\/coderbee.net\/?p=1287"},"modified":"2015-08-23T23:00:15","modified_gmt":"2015-08-23T15:00:15","slug":"%e6%9c%80%e8%bf%91%e5%a4%84%e7%90%86%e7%9a%84%e4%b8%a4%e4%b8%aa%e5%9d%91","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20150821\/1287","title":{"rendered":"\u6700\u8fd1\u5904\u7406\u7684\u4e24\u4e2a\u5751\uff1aSpring \u542f\u52a8\u95ee\u9898\u4e0e log4j \u914d\u7f6e"},"content":{"rendered":"<h2>Spring \u4f9d\u8d56\u6ce8\u5165\u95ee\u9898<\/h2>\n<p>\u8fd1\u534a\u5e74\uff0c\u5728\u751f\u4ea7\u73af\u5883\u8001\u662f\u51fa\u73b0\u5e94\u7528\u5728 JBoss \u91cc\u542f\u52a8\u4e0d\u6765\uff0c\u5728\u4e0b\u9762\u7684\u65b9\u6cd5\u8f93\u51fa\u65e5\u5fd7\u3001\u8fdb\u5165\u5faa\u73af\u540e\u5c31\u8d70\u4e0d\u51fa\u90a3\u4e2a\u5faa\u73af\u4e86\uff1a<\/p>\n<p>Spring 3.0.5\uff1a <code>org.springframework.beans.factory.support.DefaultListableBeanFactory<\/code><\/p>\n<pre><code class=\"java\">public void preInstantiateSingletons() throws BeansException {\n     if (this.logger.isInfoEnabled()) {\n          this.logger.info(\"Pre-instantiating singletons in \" + this);\n     }\n\n     synchronized (this.beanDefinitionMap) {\n          for (String beanName : this.beanDefinitionNames) {\n               RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);\n               if (!bd.isAbstract() &amp;&amp; bd.isSingleton() &amp;&amp; !bd.isLazyInit()) {\n                    if (isFactoryBean(beanName)) {\n                         final FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);\n                         boolean isEagerInit;\n                         if (System.getSecurityManager() != null &amp;&amp; factory instanceof SmartFactoryBean) {\n                              isEagerInit = AccessController.doPrivileged(new PrivilegedAction&lt;Boolean&gt;() {\n                                   public Boolean run() {\n                                        return ((SmartFactoryBean) factory).isEagerInit();\n                                   }\n                              }, getAccessControlContext());\n                         }\n                         else {\n                              isEagerInit = factory instanceof SmartFactoryBean &amp;&amp; ((SmartFactoryBean) factory).isEagerInit(); \n                         }\n                         if (isEagerInit) {\n                              getBean(beanName);\n                         }\n                    }\n                    else {\n                         getBean(beanName);\n                    }\n               }\n          }\n     }\n}\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u662f\u751f\u4ea7\u73af\u5883\uff0c\u6ca1\u6cd5\u76f4\u63a5\u8fdc\u7a0b debug\uff0c\u53ea\u80fd dump \u51fa\u6808\u548c\u5806\u6765\u5206\u6790\uff0c\u4ece\u6808\u6765\u770b\uff0cSpring \u4e00\u76f4\u5728\u505a bean \u5b9e\u4f8b\u5316\uff1b\u4ece\u5806\u6765\u770b\uff0c\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<blockquote>\n<p>Top elements include:<\/p>\n<blockquote>\n<p>\u202213,701 \u00d7 Error creating bean with name &#8216;sqlSessionFactory&#8217; &#8230; >   (496 bytes)<br \/>\n      \u202213,732 \u00d7 Could not autowire method: public final void org.m&#8230; (336 bytes)<br \/>\n    &#8230;&#8230;.\/\/\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u7684\u7c7b\u521b\u5efa\u5931\u8d25<\/p>\n<\/blockquote>\n<\/blockquote>\n<p>\u8fd9\u4e2a\u95ee\u9898\u6709\u4e00\u5b9a\u7684\u968f\u673a\u6027\uff0c\u56e0\u4e3a\u4e0d\u662f\u603b\u662f\u8d77\u4e0d\u6765\uff0c\u91cd\u542f\u591a\u6b21\u4e4b\u540e\u5c31\u53ef\u80fd\u987a\u5229\u542f\u52a8\u4e86\uff0c\u641e\u5f97\u6bcf\u6b21\u53d1\u7248\u672c\u90fd\u80c6\u6218\u5fc3\u60ca\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u6700\u8fd1\u9879\u76ee\u6539\u7248\uff0c\u65b0\u589e\u4e86\u5f88\u591a\u7c7b\uff0c\u7136\u540e\u6d4b\u8bd5\u73af\u5883\u4e5f\u51fa\u73b0\u8fd9\u6837\u7684\u95ee\u9898\u4e86\u3002\u901a\u8fc7 debug\uff0c\u6700\u7ec8\u53d1\u73b0\u662f <code>org.springframework.beans.factory.annotation.InjectionMetadata.InjectedElement<\/code> \u7c7b\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">protected void inject(Object target, String requestingBeanName, PropertyValues pvs) throws Throwable {\n     if (this.isField) {\n          Field field = (Field) this.member;\n          ReflectionUtils.makeAccessible(field);\n          field.set(target, getResourceToInject(target, requestingBeanName));\n     }\n     else {\n          if (checkPropertySkipping(pvs)) {\n               return;\n          }\n          try {\n               Method method = (Method) this.member;\n               ReflectionUtils.makeAccessible(method);\n               method.invoke(target, getResourceToInject(target, requestingBeanName));  \/\/ here\n          }\n          catch (InvocationTargetException ex) {\n               throw ex.getTargetException();\n          }\n     }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6ce8\u5165 <code>DataSource<\/code> \u65f6\u629b\u51fa\u4e86\u6808\u6ea2\u51fa\u5f02\u5e38 <code>StackOverflowError<\/code>\uff0c\u8fd9\u4e2a\u5f02\u5e38\u88ab Spring \u6355\u83b7\u5e76\u8f6c\u6362\u4e3a <code>BeanCreationException<\/code>\uff0c\u8ba4\u4e3a\u662f\u6b63\u5e38\u7684\u60c5\u51b5\uff0c\u6355\u83b7\u7684\u5730\u65b9\u662f\u7c7b <code>org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory<\/code> \u7684 <code>protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">\/\/ Initialize the bean instance.\nObject exposedObject = bean;\ntry {\n     populateBean(beanName, mbd, instanceWrapper);\n     if (exposedObject != null) {\n          exposedObject = initializeBean(beanName, exposedObject, mbd);\n     }\n}\ncatch (Throwable ex) {\n     if (ex instanceof BeanCreationException &amp;&amp; beanName.equals(((BeanCreationException) ex).getBeanName())) {\n          throw (BeanCreationException) ex;\n     }\n     else {\n          throw new BeanCreationException(mbd.getResourceDescription(), beanName, \"Initialization of bean failed\", ex);\n     }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u95ee\u9898\u6ca1\u7a7a\u7ee7\u7eed\u5206\u6790\u4e0b\u53bb\u4e86\uff0c\u8fd8\u6709\u5f85\u5f04\u660e\u767d\u7684\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u4e3a\u4ec0\u4e48\u4e00\u4e2a Bean \u4f9d\u8d56\u6ce8\u5165\u5931\u8d25\u4f1a\u5bfc\u81f4\u6b7b\u5faa\u73af\uff1f<\/li>\n<li>\u540c\u6837\u662f 64 \u4f4d\u7684 JDK\uff0c\u4e3a\u4ec0\u4e48\u5728\u751f\u4ea7\u73af\u5883\u66f4\u5bb9\u6613\u51fa\u73b0\uff0c\u800c\u6d4b\u8bd5\u73af\u5883\u7684\u9891\u7387\u4f4e\u4e9b\uff1b\u5f00\u53d1\u73af\u5883\u662f 32 \u4f4d\u7684\uff0c\u4ece\u5f15\u7528\u7684\u5927\u5c0f\u5bb9\u6613\u89e3\u91ca\u3002<\/li>\n<\/ol>\n<h2>log4j \u7684\u65e5\u5fd7\u7ea7\u522b\u95ee\u9898<\/h2>\n<p>\u6700\u8fd1\u7a81\u7136\u53d1\u73b0\u5728\u5e94\u7528\u7684\u65e5\u5fd7\u7ea7\u522b\u53d8\u6210 debug \u7ea7\u522b\u4e86\uff0c\u8f93\u51fa\u4e00\u5927\u5806\u65e5\u5fd7\uff0c\u6839\u672c\u6ca1\u6cd5\u770b\u3002\u5c1d\u8bd5\u4fee\u6539\u5e94\u7528\u63d0\u4f9b\u7684 log4j \u914d\u7f6e\u6587\u4ef6\u548c JBoss \u81ea\u5df1\u7684 log4j \u914d\u7f6e\u6587\u4ef6\u90fd\u6ca1\u6709\u6548\u679c\u3002<\/p>\n<p>\u95ee\u4e0b\u5468\u56f4\u7684\u4eba\uff0c\u90fd\u5df2\u7ecf\u77e5\u9053\u4e86\uff0c\u6ca1\u4eba\u5206\u6790\u95ee\u9898\u3002\u6709\u7684\u4eba\u8bf4\u628a log4j \u7684\u65e5\u5fd7\u7ea7\u522b\u4ece INFO \u6539\u4e3a ERROR\uff0c\u542f\u52a8\u5e94\u7528\uff0c\u505c\u6b62\uff0c\u7136\u540e\u518d\u6539\u56de INFO \u5c31\u53ef\u4ee5\u4e86\u3002\u597d\u65e0\u8bed\uff0c\u8fd9\u662f\u8df3\u5927\u795e\u5417\uff1f\uff1f\uff01\uff01<\/p>\n<p>\u770b\u4e86\u4e0b\u65e5\u5fd7\uff0c\u6709 Spring \u8f93\u51fa\u7684\uff0c\u4ece Spring \u7684\u6e90\u7801\u6765\u770b\uff0c\u5b83\u7528\u4e86 commons-logging \u7684 API\uff0c\u7136\u540e\u81ea\u5df1\u63d0\u4f9b\u4e00\u4efd commongs-logging \u7684\u914d\u7f6e\u8fdb\u53bb\u4e5f\u6ca1\u6709\u6548\u679c\u3002<\/p>\n<p>\u95ee\u4e86\u65c1\u8fb9\u90a3\u4e2a\u8ba4\u771f\u505a\u4e8b\u7684\u59b9\u5b50\uff0c\u5979\u8bf4\u662f\u4e00\u6b21\u4ee3\u7801\u5408\u5e76\u540e\u624d\u51fa\u73b0\u7684\uff0c\u90a3\u6b21\u5408\u5e76\u52a0\u4e86\u51e0\u4e2a jar \u5305\u3002\u628a\u90a3\u6b21\u65b0\u589e\u7684 jar \u90fd\u6253\u5f00\u68c0\u67e5\uff0c\u5176\u4e2d\u4e00\u4e2a\u7adf\u7136\u6709\u4e00\u4efd log4j \u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u628a org \u5f00\u5934\u7684\u5305\u7684\u65e5\u5fd7\u7ea7\u522b\u5b9a\u4e3a debug \u4e86\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><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>Spring \u4f9d\u8d56\u6ce8\u5165\u95ee\u9898 \u8fd1\u534a\u5e74\uff0c\u5728\u751f\u4ea7\u73af\u5883\u8001\u662f\u51fa\u73b0\u5e94\u7528\u5728 JBoss \u91cc\u542f\u52a8 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20150821\/1287\">\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":[18],"tags":[236,172,237],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1287"}],"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=1287"}],"version-history":[{"count":7,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1287\/revisions"}],"predecessor-version":[{"id":1296,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1287\/revisions\/1296"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}