0

我正在尝试配置和运行 spring + hibernate 客户端服务器项目(Spring's HTTP invoker)。我省略了 httpinvoker 配置和 web.xml,因为 httpinvoker 的配置运行良好。根据答案中的建议,我删除了TestSimple该类,并在尝试从客户端运行服务时发布了堆栈跟踪。

mvn tomcat:run在服务器端运行时发布了日志(如下),并在调用客户端代码后发布了堆栈跟踪(发布在下面)。我正在为 sessionFactory 获取 NPE。你能提出一些想法吗?

我的 applicationContext.xml 看起来像:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

        <!-- Database Configuration -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/WEB-INF/config.properties</value>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
        p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">${hibernate.dialect}</prop>
                <prop key="show_sql">${hibernate.show_sql}</prop>
                <prop key="format_sql">true</prop>
                <prop key="use_sql_comments">true</prop>
                <prop key="hibernate.connection.autocommit">true</prop>
                <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.gt.cbpr.model"></property>
    </bean>

    <!-- auto wired/scan annotation based config -->
    <context:annotation-config />
    <context:component-scan base-package="com.gt.cbpr.server" />
    <tx:annotation-driven />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="dataSource" ref="dataSource" />
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

我的 LoginUserServiceImpl :

package com.gt.cbpr.server.service.impl;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gt.cbpr.model.LoginUser;
import com.gt.cbpr.service.LoginUserService;

@Component
public class LoginUserServiceImpl implements LoginUserService {
    @Autowired
    private SessionFactory sessionFactory;

    public void saveLoginUser(LoginUser user) throws Exception {
        System.out.println(sessionFactory);
        sessionFactory.getCurrentSession().saveOrUpdate(user);

    }
}

客户端代码:

import com.gt.cbpr.model.LoginUser;
import com.gt.cbpr.service.LoginUserService;
import com.gt.lib.utils.BeanUtils;

public class HelloClient {

    public static void main(String[] args) throws Exception{
        LoginUserService lus = (LoginUserService) BeanUtils.getBean("loginService");
        LoginUser u  = new LoginUser();
        u.setUsername("AA");
        u.setPassword("AAAASS");
        lus.saveLoginUser(u); // I get exception here.
    }
}

tomcat 日志: http: //pastebin.com/Xw92PJv6

我运行上述客户端代码时的堆栈跟踪:

10:47:11,938 DEBUG DispatcherServlet:819 - DispatcherServlet with name 'remoting' processing POST request for [/cbpr/remoting/helloService]
10:47:11,942 DEBUG BeanNameUrlHandlerMapping:124 - Mapping [/helloService] to HandlerExecutionChain with handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@93fd01] and 1 interceptor
10:47:11,946 DEBUG HttpInvokerServiceExporter:73 - Applying RemoteInvocation: method name 'setHello'; parameter types [com.gt.cbpr.model.HelloObject]
10:47:11,950 DEBUG RemoteInvocationTraceInterceptor:66 - Incoming HttpInvokerServiceExporter remote call: com.gt.cbpr.service.HelloService.setHello
10:47:11,950 DEBUG RemoteInvocationTraceInterceptor:72 - Finished processing of HttpInvokerServiceExporter remote call: com.gt.cbpr.service.HelloService.setHello
10:47:11,956 DEBUG DispatcherServlet:957 - Null ModelAndView returned to DispatcherServlet with name 'remoting': assuming HandlerAdapter completed request handling
10:47:11,956 DEBUG DispatcherServlet:913 - Successfully completed request
10:47:11,961 DEBUG DispatcherServlet:819 - DispatcherServlet with name 'remoting' processing POST request for [/cbpr/remoting/helloService]
10:47:11,961 DEBUG BeanNameUrlHandlerMapping:124 - Mapping [/helloService] to HandlerExecutionChain with handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@93fd01] and 1 interceptor
10:47:11,962 DEBUG HttpInvokerServiceExporter:73 - Applying RemoteInvocation: method name 'getHello'; parameter types []
10:47:11,962 DEBUG RemoteInvocationTraceInterceptor:66 - Incoming HttpInvokerServiceExporter remote call: com.gt.cbpr.service.HelloService.getHello
10:47:11,963 DEBUG RemoteInvocationTraceInterceptor:72 - Finished processing of HttpInvokerServiceExporter remote call: com.gt.cbpr.service.HelloService.getHello
10:47:11,964 DEBUG DispatcherServlet:957 - Null ModelAndView returned to DispatcherServlet with name 'remoting': assuming HandlerAdapter completed request handling
10:47:11,965 DEBUG DispatcherServlet:913 - Successfully completed request
10:47:11,994 DEBUG DispatcherServlet:819 - DispatcherServlet with name 'remoting' processing POST request for [/cbpr/remoting/loginService]
10:47:11,995 DEBUG BeanNameUrlHandlerMapping:124 - Mapping [/loginService] to HandlerExecutionChain with handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@9036e] and 1 interceptor
10:47:11,997 DEBUG HttpInvokerServiceExporter:73 - Applying RemoteInvocation: method name 'saveLoginUser'; parameter types [com.gt.cbpr.model.LoginUser]
10:47:11,999 DEBUG RemoteInvocationTraceInterceptor:66 - Incoming HttpInvokerServiceExporter remote call: com.gt.cbpr.service.LoginUserService.saveLoginUser
null


10:47:12,009  WARN RemoteInvocationTraceInterceptor:80 - Processing of HttpInvokerServiceExporter remote call resulted in fatal exception: com.gt.cbpr.service.LoginUserService.saveLoginUser
java.lang.NullPointerException
        at com.gt.cbpr.server.service.impl.LoginUserServiceImpl.saveLoginUser(LoginUserServiceImpl.java:25)
        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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:70)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at $Proxy26.saveLoginUser(Unknown Source)
        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.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:205)
        at org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:38)
        at org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:76)
        at org.springframework.remoting.support.RemoteInvocationBasedExporter.invokeAndCreateResult(RemoteInvocationBasedExporter.java:112)
        at org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.handleRequest(HttpInvokerServiceExporter.java:117)
        at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:662)
10:47:12,020 DEBUG HttpInvokerServiceExporter:92 - Target method failed for RemoteInvocation: method name 'saveLoginUser'; parameter types [com.gt.cbpr.model.LoginUser]
java.lang.NullPointerException
        at com.gt.cbpr.server.service.impl.LoginUserServiceImpl.saveLoginUser(LoginUserServiceImpl.java:25)
        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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:70)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at $Proxy26.saveLoginUser(Unknown Source)
        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.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:205)
        at org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:38)
        at org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:76)
        at org.springframework.remoting.support.RemoteInvocationBasedExporter.invokeAndCreateResult(RemoteInvocationBasedExporter.java:112)
        at org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.handleRequest(HttpInvokerServiceExporter.java:117)
        at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:662)
10:47:12,064 DEBUG DispatcherServlet:957 - Null ModelAndView returned to DispatcherServlet with name 'remoting': assuming HandlerAdapter completed request handling
10:47:12,067 DEBUG DispatcherServlet:913 - Successfully completed request

我不知道出了什么问题。你能帮我吗?

4

4 回答 4

4

只是一个猜测,但你是saveUser从构造函数调用的。我不确定spring,但是在我使用的其他CDI中,bean不是在创建对象时注入的,而是在创建对象之后注入的。

对于这种“初始化”,您应该使用saveUserwith @PostConstruct(在注入依赖项后执行该方法)。该方法不应带任何参数。当然,您应该从构造函数中删除方法调用。

于 2012-05-28T20:23:04.523 回答
3

您显示的日志是您的 Web 应用程序的日志。您的测试是一个独立的应用程序,其中主要方法只是使用new. Spring 无法自动装配不实例化自身的 bean。当你使用 new 构造一个 bean 时,你完全绕过了 Spring。

您的 main 方法需要实例化一个应用程序上下文,并向该上下文询问 type 的 bean TestSimple

@SJUan76 的答案也是正确的。Spring 实例化 bean ,然后自动装配它。所以在构造函数中,还没有自动装配任何东西。在构造函数中执行初始化以外的操作通常是一种不好的做法。

于 2012-05-28T20:27:48.940 回答
1

您没有将 TestSimple 拉出 Spring 上下文,因此 @Autowired 标记什么也不做。在您的主体中,将对象从弹簧上下文中拉出来,这会更好。

于 2012-05-28T20:28:15.500 回答
1

如果您正在编写集成测试来测试应用程序容器中 bean 的行为,那么您应该考虑使用Spring TestContext 框架(这是为 Spring 编写集成测试的官方方法) - 没有魔法 - 通常 JUnit 将运行 Spring Test实现 JUnit Class Runner Api 的上下文类。因此,您运行 JUnit,JUnit 运行 TestContext Framework,Test Context Framework 从指定的 XML 配置文件创建应用程序上下文并运行您的测试。简单的。

单元测试是另一回事,你可以通过模拟它们的依赖项来分别测试你的 bean。在这一部分中,您必须手动注入 bean 的依赖项 - 没有 DI 容器,因此您必须直接从测试类调用构造函数和属性设置器。这也可能意味着您必须用公共 @Autowired 属性设置器替换您的 @Autowired 字段。

于 2012-05-28T21:12:44.313 回答