0

Autowired アノテーションを機能させるのに問題があります。次の 2 つのクラスを参照してください。

@Component("upAuthenticationProvider")
public class UPAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private AuthenticationDAO authenticationDAO;

    // snip

    public AuthenticationDAO getAuthenticationDAO() {
    return authenticationDAO;
    }

    public void setAuthenticationDAO(
          AuthenticationDAO authenticationDAO) {
    this.authenticationDAO = authenticationDAO;
    }
}

および依存関係:

@Repository
public class AuthenticationPostgresDAO implements AuthenticationDAO{

    @Autowired
    private DataSource dataSource;

    // snip...
}

アプリが初期化されると、ログに次のように表示されます。

08:09:41.268 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'upAuthenticationProvider'
08:09:41.268 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'upAuthenticationProvider'
08:09:41.269 [pool-2-thread-1] DEBUG o.s.b.f.annotation.InjectionMetadata - Found injected element on class [com.mycompany.authentication.UPAuthenticationProvider]: AutowiredFieldElement for private com.mycompany.dao.AuthenticationDAO com.mycompany.authentication.UPAuthenticationProvider.AuthenticationDAO
08:09:41.269 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'upAuthenticationProvider' to allow for resolving potential circular references
08:09:41.271 [pool-2-thread-1] DEBUG o.s.b.f.annotation.InjectionMetadata - Processing injected method of bean 'upAuthenticationProvider': AutowiredFieldElement for private com.mycompany.dao.AuthenticationDAO com.mycompnay.authentication.UPAuthenticationProvider.AuthenticationDAO
08:09:41.273 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'AuthenticationPostgresDAO'
08:09:41.273 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'AuthenticationPostgresDAO'
08:09:41.273 [pool-2-thread-1] DEBUG o.s.b.f.annotation.InjectionMetadata - Found injected element on class [com.mycompnay.dao.AuthenticationPostgresDAO]: AutowiredFieldElement for private javax.sql.DataSource com.mycompany.dao.AuthenticationPostgresDAO.dataSource
08:09:41.273 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'authenticationPostgresDAO' to allow for resolving potential circular references
08:09:41.273 [pool-2-thread-1] DEBUG o.s.b.f.annotation.InjectionMetadata - Processing injected method of bean 'authenticationPostgresDAO': AutowiredFieldElement for private javax.sql.DataSource com.mycompnay.dao.AuthenticationPostgresDAO.dataSource
08:09:41.274 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'dataSource'
08:09:41.274 [pool-2-thread-1] DEBUG o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'AuthenticationPostgresDAO' to bean named 'dataSource'
08:09:41.274 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'authenticationPostgresDAO'
08:09:41.274 [pool-2-thread-1] DEBUG o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'upAuthenticationProvider' to bean named 'authenticationPostgresDAO'
08:09:41.274 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'upAuthenticationProvider'
08:09:41.274 [pool-2-thread-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'authenticationPostgresDAO'

依存関係を作成して接続したと言っているようですが、私のアプリケーションでは、upAuthenticationProvider から authencticationPostgresDAO にアクセスしようとすると NPE が発生します。

4

1 に答える 1

0

したがって、問題は明らかにあなたが示したコードにはありません... 1)upAuthenticationProviderのインスタンスにどのようにアクセスしますか。Spring ApplicationContextから取得する必要があります。2) NPE をスローするコードは何ですか? 例外をスローする行で、DAO 以外の何かが null になる可能性はありますか?

于 2012-05-25T12:41:26.720 に答える