getting NullPointerException whill accessing a web

2019-07-24 23:39发布

I have been writing a project who acts as a server and have services up and running available to be consumed by client side. My Server application is a Simple CRUD application based on Spring MVC 3 framework. over records of a single table named Person. My WebServices are SOAP1.1 servcies and are live when once I deploy my project on TomCat. I can access its wsdl on localhost and I can these webservices by running and deploying that project over tomcat. I have tested serives by retuning flags from Service methods. Now issue is that. If I run Server application over the web through its controller, I am able to do crud through web interface properly. All the components Repository, Service DAO's are properly annotated and works over web. But when I try to do same work through my services. I get NullPointerExceptions for Autowired objects.

Following is an exception. Please help and through your thoughts in comments.

WARN org.apache.cxf.phase.PhaseInterceptorChain  - Application {http://service.accounting.cassit.com/}personService#{http://service.accounting.cassit.com/}deletePerson has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault
    at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:155)
    at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.createFault(AbstractJAXWSMethodInvoker.java:86)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:121)
    at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:61)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:75)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
    at org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:102)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:464)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:188)
    at org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:103)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
    at com.cassit.accounting.service.PersonServiceImpl.deletePerson(PersonServiceImpl.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:173)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:89)

When look at this line and inspected line number 47 or respective java file. I come to know that I was accessing an AutoWired property. Why is it so?

Caused by: java.lang.NullPointerException
        at com.cassit.accounting.service.PersonServiceImpl.deletePerson(PersonServiceImpl.java:47)

best regards, aqif

2条回答
不美不萌又怎样
2楼-- · 2019-07-25 00:11

Spring dependency injection works only in components managed by Spring. If your class is not managed by Spring, @Autowired would not work. Check it it is declared as a Spring bean (using <bean> or annotation).

查看更多
地球回转人心会变
3楼-- · 2019-07-25 00:12

See Section 4.9, Annotation-based container configuration, in the Spring Reference.

The important bit is the note right before Section 4.9.1, quoting:

<context:annotation-config/> only looks for annotations on beans in the same application context in which it is defined. This means that, if you put <context:annotation-config/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Autowired beans in your controllers, and not your services. See Section 16.2, “The DispatcherServlet” for more information.

It is likely your @Service-annotated classes are not being scanned; try:

<context:component-scan base-package="your.service.package" />
查看更多
登录 后发表回答