Spring 3 Autowired注释无法正常工作

时间:2012-05-25 12:34:37

标签: spring annotations autowired

我在使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'

看起来它告诉我它创建了依赖关系并将其连接起来,但是在我的应用程序中,我尝试从upAuthenticationProvider访问authencticationPostgresDAO时得到一个NPE

1 个答案:

答案 0 :(得分:0)

所以问题显然不在你告诉我们的代码中...... 1)如何访问upAuthenticationProvider的实例,你应该从Spring ApplicationContext获取它。 2)抛出NPE的代码是什么?在抛出异常的行上DAO是否为null?