2011-12-21 15 views
2

私はBeanのメソッドを使用するクラスを持っています。Autowiredフィールドがnull例外を返します

私は@Autowiredを使用して、私のクラスにそのメソッドを注入しようとしているが、それは私にNullPointerExceptionができます。

public class ChangePasswordServiceImpl extends RemoteServiceServlet implements 
     ChangePasswordService { 

    @Autowired 
    @Qualifier("userDetailsManager") 
    private JdbcUserDetailsManager userDetailsManager; 

    @Override 
    public void changePassword(String oldPassword, String newPassword) { 

     // This is the line that throws NullPointerException, i.e., userDetails 
     // Manager is not being injected by Spring 
     userDetailsManager.changePassword(oldPassword, newPassword); 

    } 

} 

マイXMLファイル:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:context="http://www.springframework.org/schema/context" 

    xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
      http://www.springframework.org/schema/jdbc 
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:annotation-config/> 

    <bean id="securityDataSource" 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/login" /> 
     <property name="username" value="root" /> 
     <property name="password" value="password" /> 
    </bean> 

    <bean id="userDetailsManager" 
     class="org.springframework.security.provisioning.JdbcUserDetailsManager"> 
     <property name="dataSource" ref="securityDataSource" /> 
     <property name="authenticationManager" ref="authenticationManager" /> 
     <qualifier value="userDetailsManager"/> 
    </bean> 

</beans> 

はなぜこのフィールドは満たされていませんか?何か不足していますか? ChangePasswordServiceImpl以来

+0

どのように 'ChangePasswordServiceImpl'がインスタンス化されますか?それはどのように注釈付けされていますか? –

+0

うーん、わかりません。このクラスは、GWTからのRPCサービスの実装です。注釈を付ける必要がありますか?私はそれをインスタンス化するGWTだと信じています。 –

+1

あなたは(http://code.google.com/p/spring4gwt/)やSpringの一部/ GWTのチュートリアルが、うん、あなたが不足している何か[spring4gwt]で見たいと思うかもしれません:) –

答えて

2

は、あなたがしようとしたときにNullPointerExceptionを取得するようになりますあなたのクラスのために何もしません@Autowired、(そのためには、ApplicationContextの一部ではない、それはXMLの設定がされていません)春マネージクラスではありませんクラスのインスタンスを使用する。

現在ある残りと共に、あなたのXML構成でChangePasswordServiceImplためのBeanを定義する必要があります。

<bean id="changePasswordService" class="the.package.ChangePasswordServiceImpl"/> 

最後に、ApplicationContextで保持することができます。

//assuming you are holding onto an instance of the ApplicationContext 
ChangePasswordServiceImpl service = appContext.getBean("changePasswordService", ChangePasswordServiceImpl.class); 
service.changePassword("old", "new"); 
+0

フム、春ので、すべてのクラスで '@ Autowired'を探すのではなく、' ApplicationContext'にあるものだけを探します。それはGWT RPC Service Implementationクラスであり、私はそれをインスタンス化しません(GWTはそれを行います)、代わりに通常のServletを使用する方が簡単だろうと思います。 –

+0

Beanは、Springで管理するためにXML構成にする必要はありません。注釈を付けることができます。 –

+0

はい、xmlにする必要はありませんが、質問で提供された情報に基づいて、私は彼がxml経由で排他的に配線しようとしていると仮定しました。 @jdanielnd:どのようにgwtがうまくいくかはわかりませんが、アプリケーションのweb.xmlデプロイメント記述子がある場合は、そこにアプリケーションコンテキストを設定できます。この作業の詳細については、このリンクを参照してください。http://www.ibm.com/developerworks/web/library/wa-spring3/ –

関連する問題