我正在尝试配置和运行spring + hibernate客户端服务器项目(Spring's HTTP invoker
)。
我省略了httpinvoker配置和web.xml,因为httpinvoker的配置运行良好。
根据答案的建议,当我尝试从客户端运行服务时,我删除了TestSimple
类,并发布了栈跟踪。
我在服务器端运行mvn tomcat:run
时发布了log(下面),在调用客户端代码后发布了stacktrace(下面发布)。我正在获得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
我无法弄清楚发生了什么错误。你能帮帮我吗?
答案 0 :(得分:4)
只是一个猜测,但你是从构造函数中调用saveUser
。我不确定spring,但在其他CDI我使用过的bean在创建对象时没有注入,但是在创建了对象之后。
对于这种“初始化”,您应该将saveUser
与@PostConstruct
一起使用(将在注入依赖项后执行该方法)。该方法不应采用任何参数。当然,您应该从构造函数中删除方法调用。
答案 1 :(得分:3)
您显示的日志是您的Web应用程序的日志。您的测试是一个独立的应用程序,其中main方法使用new
简单地实例化您的测试类。 Spring没有办法自动装配一个它没有实例化的bean。使用new构造bean时,你完全绕过了Spring。
您的main方法需要实例化应用程序上下文,并向此上下文询问类型TestSimple
的bean。
@ SJUan76的anwser也是正确的。 Spring实例化bean,然后自动装配它。所以在构造函数中,还没有任何自动装配。除了在构造函数中进行初始化之外,执行其他操作通常是一种不好的做法。
答案 2 :(得分:1)
你没有将TestSimple从Spring上下文中拉出来,所以@Autowired标签什么都不做。在你的主体中,将对象从弹簧上下文中拉出来,这样可以更好地工作。
答案 3 :(得分:1)
如果您正在编写集成测试来测试应用程序容器中bean的行为,那么您应该考虑使用Spring TestContext framework(这是为Spring编写集成测试的官方方式) - 没有任何魔力 - 通常JUnit将运行实现JUnit Class Runner Api的Spring Test Context类。因此,运行JUnit,JUnit运行TestContext Framework,Test Context Framework从指定的XML配置文件创建应用程序上下文并运行测试。简单。
单元测试是另一个故事,你可以通过模拟它们的依赖项来单独测试你的bean。在这一部分中,您必须手动注入bean的依赖项 - 没有DI容器,因此您必须直接从测试类中调用构造函数和属性设置器。这也许意味着您必须将@Autowired字段替换为公共@Autowired属性设置器。