1. 程式人生 > >OSGI入職訓練之 org.springframework.beans.BeanInstantiationException

OSGI入職訓練之 org.springframework.beans.BeanInstantiationException

目前職級:初級

問題水平:初級

問題現狀:[ims] 16-06-30 23:03:35:0181 ERROR CommonsLogger.error(38) | Exception occurred during processing request: Unable to instantiate Action,

com.troy.ods.system.action.TestAction,  defined for 'test' in namespace '/system'Error creating bean with name 'com.troy.ods.system.action.TestAction': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.system.action.TestAction]: Constructor threw exception; nested exception is java.lang.NullPointerException

Unable to instantiate Action, com.troy.ods.system.action.TestAction,  defined for 'test' in namespace '/system'Error creating bean with name 'com.troy.ods.system.action.TestAction': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.system.action.TestAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:316)
    at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at com.troy.framework.tools.struts.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:77)
    at com.troy.framework.core.interceptor.StrutsPrepareAndExecuteFilterEx.doFilter(StrutsPrepareAndExecuteFilterEx.java:41)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.troy.ods.system.action.TestAction': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.system.action.TestAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowire(AbstractAutowireCapableBeanFactory.java:350)
    at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:179)
    at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)
    at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:171)
    at com.opensymphony.xwork2.factory.DefaultActionFactory.buildAction(DefaultActionFactory.java:22)
    at com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
    ... 27 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.system.action.TestAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:125)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:270)
    ... 35 more
Caused by: java.lang.NullPointerException
    at com.troy.framework.core.util.ParamUtil.getAllParamMap(ParamUtil.java:75)
    at com.troy.framework.core.action.BaseAction.getRequestParamsMap(BaseAction.java:253)
    at com.troy.framework.core.action.BaseAction.getDataObject(BaseAction.java:354)
    at com.troy.ods.system.action.TestAction.<init>(TestAction.java:18)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 37 more
[ims] 16-06-30 23:03:35:0221 INFO StrutsPrepareAndExecuteFilterEx.doFilter(44) | 過濾鏈共花時間:103ms
[ims] 16-06-30 23:03:40:0631 ERROR CommonsLogger.error(38) | Exception occurred during processing request: Unable to instantiate Action, com.troy.ods.crud.action.CrudAction,  defined for 'crud' in namespace '/crud'Error creating bean with name 'com.troy.ods.crud.action.CrudAction': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.crud.action.CrudAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
Unable to instantiate Action, com.troy.ods.crud.action.CrudAction,  defined for 'crud' in namespace '/crud'Error creating bean with name 'com.troy.ods.crud.action.CrudAction': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.crud.action.CrudAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:316)
    at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
    at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:552)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at com.troy.framework.tools.struts.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:77)
    at com.troy.framework.core.interceptor.StrutsPrepareAndExecuteFilterEx.doFilter(StrutsPrepareAndExecuteFilterEx.java:41)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.troy.ods.crud.action.CrudAction': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.crud.action.CrudAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:278)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowire(AbstractAutowireCapableBeanFactory.java:350)
    at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:179)
    at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:154)
    at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:171)
    at com.opensymphony.xwork2.factory.DefaultActionFactory.buildAction(DefaultActionFactory.java:22)
    at com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:141)
    at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
    ... 27 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.troy.ods.crud.action.CrudAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:125)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:270)
    ... 35 more
Caused by: java.lang.NullPointerException
    at com.troy.framework.core.util.ParamUtil.getAllParamMap(ParamUtil.java:75)
    at com.troy.framework.core.action.BaseAction.getRequestParamsMap(BaseAction.java:253)
    at com.troy.framework.core.action.BaseAction.getDataObject(BaseAction.java:354)
    at com.troy.ods.crud.action.CrudAction.<init>(CrudAction.java:25)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)

預判原因:bean例項化問題,主頁hello world可以訪問。預判spring異常造成

實際原因:action中設定了錯誤的Idataobj為全域性變數,導致其獲取引數為空。進而導致action無法完成例項化

解決方案:將action中的idataobj寫在每個方法的裡面,然後進行除錯成功。

心得:從最簡系統做起,嚴格控制變數數量減少錯誤可能數量。方便查詢