@Autowired上的webapp中的Nullpointer

时间:2015-03-02 17:40:01

标签: java spring hibernate jpa

所以我正在添加一个Spring 3自动装配&我的项目的JPA / Hibernate数据访问层,其中webapp和数据部分分隔在不同的war / jar中。尽管如此,webapp确实将数据jar包含为Maven中的依赖项。 JUnit中的数据端一切正常,部署没有错误,但是当页面加载并且正在执行对自动装配的DAO bean的调用时,我得到一个NullPointerException。

踢球者是,我没有设计webapp部分,但我确实可以根据需要修改它以使其工作。我有一种感觉,这与webapp仍然没有看到自动装配DAO我试图实例化,即使数据jar被添加为依赖。

是否需要在webapps applicationContext中添加某些内容才能同步这些内容?我对此很陌生,所以我肯定我错过了一些非常明显的东西!我看过类似的问题,但似乎没有任何东西符合我目前的情况。

我的数据applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
                           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="persistenceUnitName" value="ApplicationEntityManager" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="***" />
        <property name="username" value="***" />
        <property name="password" value="***" />
    </bean>

    <context:component-scan base-package="...data.dao"/>
    <context:component-scan base-package="...data.service"/>

</beans>

他们的webapp“applicationContext(Dispatcher-Servlet)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="...web.controllers" />

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>  

    <bean name="/mypractice/*" class="...web.controllers.MyPracticeController">     
    </bean>

     <bean name="/milestones/*" class="...web.controllers.MilestonesController">
     </bean>

     <bean name="/resources/*" class="...web.controllers.ResourcesController">
     </bean>

     <bean name="/file/*" class="...web.controllers.FilesController">
     </bean>    

     <bean id="uploader" class="...web.service.FileUploadRestService">
     </bean>

     <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">      
    </bean>

    <bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer">
    <property name="definitions">
        <list>
        <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
    </bean>

    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

    <mvc:annotation-driven />

</beans>

他们的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">


    <display-name>Webapp</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

堆栈跟踪

[3/2/15 13:47:50:981 EST] 0000006e SystemOut     O In myPractice Controller
[3/2/15 13:47:50:982 EST] 0000006e SystemOut     O About to call findPracticeByPracticeId...
[3/2/15 13:47:51:004 EST] 0000006e SystemOut     O in my practice controller [[]]
[3/2/15 13:47:50:982 EST] 0000006e SystemErr     R java.lang.NullPointerException
[3/2/15 13:47:50:984 EST] 0000006e SystemErr     R  at ....web.service.MyPracticeService.retrievPracticeDetails(MyPracticeService.java:51)
[3/2/15 13:47:50:984 EST] 0000006e SystemErr     R  at ....web.controllers.MyPracticeController.practicesList(MyPracticeController.java:42)
[3/2/15 13:47:50:985 EST] 0000006e SystemErr     R  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[3/2/15 13:47:50:986 EST] 0000006e SystemErr     R  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[3/2/15 13:47:50:987 EST] 0000006e SystemErr     R  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[3/2/15 13:47:50:987 EST] 0000006e SystemErr     R  at java.lang.reflect.Method.invoke(Method.java:611)
[3/2/15 13:47:50:988 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:471)
[3/2/15 13:47:50:989 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:408)
[3/2/15 13:47:50:989 EST] 0000006e SystemErr     R  at ....web.common.BaseController.handleRequest(BaseController.java:32)
[3/2/15 13:47:50:990 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
[3/2/15 13:47:50:990 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
[3/2/15 13:47:50:990 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
[3/2/15 13:47:50:991 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
[3/2/15 13:47:50:992 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816)
[3/2/15 13:47:50:992 EST] 0000006e SystemErr     R  at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
[3/2/15 13:47:50:993 EST] 0000006e SystemErr     R  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
[3/2/15 13:47:50:994 EST] 0000006e SystemErr     R  at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[3/2/15 13:47:50:994 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1230)
[3/2/15 13:47:50:995 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
[3/2/15 13:47:50:995 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
[3/2/15 13:47:50:996 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[3/2/15 13:47:50:996 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1071)
[3/2/15 13:47:50:997 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
[3/2/15 13:47:50:997 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:914)
[3/2/15 13:47:50:998 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
[3/2/15 13:47:50:998 EST] 0000006e SystemErr     R  at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
[3/2/15 13:47:50:999 EST] 0000006e SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:459)
[3/2/15 13:47:50:999 EST] 0000006e SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:526)
[3/2/15 13:47:51:000 EST] 0000006e SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:312)
[3/2/15 13:47:51:000 EST] 0000006e SystemErr     R  at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
[3/2/15 13:47:51:001 EST] 0000006e SystemErr     R  at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
[3/2/15 13:47:51:001 EST] 0000006e SystemErr     R  at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[3/2/15 13:47:51:002 EST] 0000006e SystemErr     R  at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[3/2/15 13:47:51:002 EST] 0000006e SystemErr     R  at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[3/2/15 13:47:51:002 EST] 0000006e SystemErr     R  at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
[3/2/15 13:47:51:003 EST] 0000006e SystemErr     R  at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[3/2/15 13:47:51:003 EST] 0000006e SystemErr     R  at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
[3/2/15 13:47:51:004 EST] 0000006e SystemErr     R  at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1864)

1 个答案:

答案 0 :(得分:0)

应用程序启动时没有错误,这意味着没有依赖性问题。当控制器调用它调用autowire变量时,问题是该变量是如何创建的。检查构造函数并检查变量autowired类是否也被注释。 e.g

@Component
public class className{}

@Service
public class className{}