2012-05-09 9 views
0

私はSpringで新しく、sessionFactoryを注入したいと思っています。私はWebサーバーのJettyを起動していて、コンテキストをロードします。私はGWT Webアプリケーションを起動した後、私はサーバー側で呼び出しを行い、いくつかの情報を取得しようとしますが、私はNULLポインタを取得します。エラーはまったくないので、問題がどこにあるかを知ることが難しくなります。私は以前働いていたプロジェクトでそれが動作しているのを見たので、仕事をすると思っています。どんな助けでも感謝します。 (私の可能性の悪い英語のため申し訳ありませんが)ここでsessionFactoryの注入が動作しない

はcontext.xmlにある:ここでは

<?xml version="1.0" encoding="UTF-8" ?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:security="http://www.springframework.org/schema/security" 
    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/aop http://www.springframework.org/schema 
    /aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/security http://www.springframework.org/schema 
    /security/spring-security-3.0.xsd"> 


    <!-- Standard spring initialization --> 
    <context:component-scan base-package="com.test"> 

    </context:component-scan> 

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

    <!-- Connection to the database--> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy- 
    method="close"> 

    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/test" /> 
    <property name="username" value="root" /> 
    <property name="password" value="123456789" /> 
    </bean> 

    <!-- Hibernate session factory--> 
    <bean id="jpaSessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="com.domain"/> 
    <property name="namingStrategy" > 
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy" /> 
    </property> 
    <property name="hibernateProperties"> 
    <props> 
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
     <prop key="show_sql">true</prop> 
     <prop key="hibernate.hbm2ddl.auto">update</prop> 
    </props> 
    </property> 
    </bean> 

    <!-- Hibernate session factory --> 
    <bean id="txManager"  
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="jpaSessionFactory" /> 
    </bean> 

</beans> 

がメインです:ここでは

public static void main(String[] args) { 

     ContextHandlerCollection contexts = new ContextHandlerCollection(); 

     contexts.setHandlers(new Handler[] 
     { new AppContextBuilder().buildWebAppContext()}); 

     final JettyServer jettyServer = new JettyServer(); 
     jettyServer.setHandler(contexts); 
     Runnable runner = new Runnable() { 
      @Override 
      public void run() { 
       new ServerRunner(jettyServer); 
      } 
     }; 
     EventQueue.invokeLater(runner); 

     new ClassPathXmlApplicationContext("context.xml"); 
    } 

は、私は、注射をするクラスのテストです:

アプリケーションからのサーバー側です(完全には表示されていません)

/*** The server side implementation of the RPC service. 
*/ 
@SuppressWarnings("serial") 
public class GreetingServiceImpl extends RemoteServiceServlet implements 
    GreetingService { 

@Resource 
private TEST t; 

public String greetServer(String input) throws IllegalArgumentException { 
    // Verify that the input is valid. 
    if (!FieldVerifier.isValidName(input)) { 
     // If the input is not valid, throw an IllegalArgumentException 
         back to 
     // the client. 
     throw new IllegalArgumentException(
       "Name must be at least 4 characters long"); 
    } 

    t.getPersons(); // NULL pointer here 

.........................

それが必要のように、すべてのスキャンがように見えるので、MySQLのテーブルは、作成されます作業。

おかげ

ボブ

+0

クラスを正しいパッケージに配置していますか? (com.test)を試しましたか? –

+0

こんにちはミゲル、log4jは、すべての豆が作成されることを示しています。私はを追加しましたが、何も変更されません。ありがとう。 –

答えて

1

GreetingServiceImplクラスは、スプリングによって作成されていないので、これがある - それは直接コンテナによって初期化されるサーブレットです。このarticleの指示に従って問題を解決してください。記事の関連コードをコピーしました。

@Override 
public void init() throws ServletException { 
    super.init(); 

    final WebApplicationContext ctx = 
     WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 

    if (ctx == null) { 
     throw new IllegalStateException("No Spring web application context found"); 
    } 

    ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, 
     AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); 
} 
+0

こんにちは、それは動作しません。変数 "ctx"はNULLです。 –

+0

これを動作させるには、Spring ContextLoaderListenerを使用してコンテキストを初期化する必要があります。通常はweb.xmlで定義されています。埋め込まれたjettyサーバでどのように行うかを知ることができます。 – gkamal

+0

これは私が実際には得られないものです。あなたはそのコンセプトを説明する参考資料を知っていますか?ありがとう。 –

関連する問題