2012-05-25 125 views
0

我在使Autowired註釋工作時遇到了一些麻煩。看到這兩個類:Spring 3 Autowired註解不起作用

@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' 

它看起來像它告訴我,它創建了依賴關係並將其連接起來,但在我的應用程序中,當嘗試訪問驗證PostgreSQL從upAuthenticationProvider獲得NPE時

+0

您的applicationContext.xml文件中是否包含? – BenSchro10

+0

@ BenSchro10是的,我有他們兩個設置。 –

回答

0

所以問題顯然不在您向我們展示的代碼中...... 1)如何訪問您的upAuthenticationProvider實例,您應該從Spring ApplicationContext獲取它。 2)什麼是拋NPE的代碼?除了DAO之外的其他東西可以在拋出異常的行上爲null?

+0

我正在使用spring安全名稱空間爲驗證管理器提供upAuthenticationProvider,因此: [code] [code] –

+0

抱歉 - 點擊輸入,然後無法編輯。這裏就是我想鍵入: 我使用Spring Security的命名空間提供upAuthenticationProvider向認證管理這樣: '<認證管理器> <認證供應商REF =「upAuthenticationProvider」 /> ' 我也使用'form-login',所以我假設所有正常的活動都在執行該方法。 NPE發生在使用'authenticationDAO'的線上,但這是一個很好的觀點。我在那一行設置了一個斷點,並且在那一點上,類成員'authenticationDAO'爲null。 –