2012-05-25 2 views
0

У меня возникли проблемы с работой аннотации Autwired. Смотрите эти два класса:Spring 3 Автообновлено аннотация не работает

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

Похоже, это говорит мне, что создал и в моем приложении, я получаю NPE при попытке получить доступ к аутентификацииPostgresDAO от upAuthenticationProvider

+0

У вас есть: и в файле applicationContext.xml? – BenSchro10

+0

@ BenSchro10 да, у меня есть оба набора. –

ответ

0

Таким образом, проблема явно не в коде, который вы нам показываете ... 1) Как получить доступ к вашему экземпляру upAuthenticationProvider, вы должны получить его из Spring ApplicationContext. 2) Что такое код, бросающий NPE? может ли что-то другое, чем DAO, быть нулевым на линии, бросающей исключение?

+0

Я использую пространство имен безопасности весны для доставки upAuthenticationProvider в диспетчер аутентификации как таковой: [код] <идентификатор-менеджер> <идентификатор-провайдер ref = "upAuthenticationProvider" /> [code] –

+0

Извините - нажмите enter, а затем не можете редактировать. Вот что я имел в виду, чтобы напечатать: Я использую пространство имена пружинной безопасности для обеспечения upAuthenticationProvider менеджера аутентификации как таковые: '<аутентификации менеджера> <аутентификации поставщик исй =«upAuthenticationProvider»/> ' Я также использую' form-login', поэтому я бы предположил, что вся нормальная активность выполняется для выполнения метода. NPE происходит на линии, использующей 'authenticationDAO', но это хороший момент. Я установил точку останова в этой строке, и член класса «authenticationDAO» имеет значение null в этой точке. –

Смежные вопросы