0

spring + hibernate クライアント サーバー プロジェクト ( Spring's HTTP invoker) を構成して実行しようとしています。httpinvoker の構成はうまく機能しているため、httpinvoker の構成と web.xml は省略しました。回答からの提案によると、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、コンストラクターから呼び出しています。春についてはよくわかりませんが、他のCDIで使用したのは、オブジェクトの作成時ではなく、オブジェクトの作成後にBeanが注入されることです。

この種の「初期化」には、saveUserwithを使用する必要があります@PostConstruct(依存関係が注入された後にメソッドを実行します)。メソッドはパラメータを取りません。そしてもちろん、コンストラクターからメソッド呼び出しを削除する必要があります。

于 2012-05-28T20:23:04.523 に答える
3

表示されるログは、Web アプリケーションのログです。テストはスタンドアロン アプリケーションであり、メイン メソッドは を使用してテスト クラスをインスタンス化するだけnewです。Spring がそれ自体をインスタンス化しない Bean を自動配線する方法はありません。new を使用して Bean を構築すると、Spring を完全にバイパスします。

メイン メソッドは、アプリケーション コンテキストをインスタンス化し、このコンテキストに type の Bean を要求する必要がありますTestSimple

@SJUan76 さんの回答も正しいです。Spring は Bean をインスタンス化し、それを自動配線します。したがって、コンストラクターでは、まだ何も自動配線されていません。一般に、コンストラクターで初期化以外のことを行うのは悪い習慣です。

于 2012-05-28T20:27:48.940 に答える
1

アプリケーション コンテナー内の Bean の動作をテストするための統合テストを作成している場合は、Spring TestContext フレームワークの使用を検討する必要があります(Spring の統合テストを作成する公式の方法です) - 魔法はありません - 通常、JUnit は Spring Test を実行しますJUnit Class Runner Api を実装するコンテキスト クラス。したがって、JUnit を実行すると、JUnit が TestContext フレームワークを実行し、Test Context Framework が指定された XML 構成ファイルからアプリケーション コンテキストを作成し、テストを実行します。単純。

単体テストは別の話です。依存関係をモックすることで、Bean を個別にテストします。この部分では、Bean の依存関係を手動で注入する必要があります。DI コンテナーがないため、コンストラクターとプロパティ セッターをテスト クラスから直接呼び出す必要があります。また、これはおそらく @Autowired フィールドを public @Autowired プロパティ セッターに置き換える必要があることを意味します。

于 2012-05-28T21:12:44.313 に答える
1

Spring コンテキストから TestSimple をプルしていないため、@Autowired タグは何もしません。あなたのメインでは、代わりにスプリングコンテキストからオブジェクトを引き出してください。これはよりうまく機能します。

于 2012-05-28T20:28:15.500 に答える