Announcement Announcement Module
Collapse
No announcement yet.
Why beans are not available in setApplicationContext() method or beans order creation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why beans are not available in setApplicationContext() method or beans order creation

    Hello.
    I'm trying to use my own custom authentication provider the same way as it is done in the Greenhouse application.
    @Service
    public class UsernamePasswordAuthenticationProvider implements AuthenticationProvider, ApplicationContextAware {

    private AuthenticationService authenticationService;

    private ApplicationContext applicationContext;

    @Inject
    public UsernamePasswordAuthenticationProvider(Authenticat ionService authenticationService) {
    this.authenticationService = authenticationService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    ...
    }
    ...
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;

    // AccountRepository accountRepository = applicationContext.getBean(AccountRepository.class );
    // authenticationService = applicationContext.getBean(AuthenticationService.c lass);

    }
    }
    These both calls throw Exceptions:
    AccountRepository accountRepository = applicationContext.getBean(AccountRepository.class );
    authenticationService = applicationContext.getBean(AuthenticationService.c lass);
    I use Spring Data JPA, and it seems that AccountRepository is not available at this moment, and AuthenticationService can't be instantiated, because constructor injection is failed (AccountRepository is @Inject-ed through AuthenticationService's constructor).

    And this is the case why UsernamePasswordAuthenticationProvider can't be instantiated too (but it works fine for the Greenhouse application).

    More interesting that everything works fine for JUnit test (all dependencies are successfully set through constructor injection), and in the UsernamePasswordAuthenticationProvider.authenticat e() method all beans are also available from the applicationContext.

    So, why Container doesn't create AccountRepository at first, then assign it to AuthenticationService through constructor injection, and then assign AuthenticationService into UsernamePasswordAuthenticationProvider (as it does for JUnit test) for the real web application?

    I use XML+namespaces+@Component's annotation (@Repository, @Service) and @Inject for the application's configuration, and I suppose configuration is correct. Why my custom authentication provider can't be instantiated properly, and why it works for the Greenhouse application (my own application is very very based on the Greenhouse sample with minor differences).

    For additional information you can see http://forum.springsource.org/showth...a-regular-bean.

  • #2
    Ok, Rob Winch helped me, he mentioned that this was according to the <debug/> usage issue. Watch for fix here https://jira.springsource.org/browse/SEC-1885.

    Comment

    Working...
    X