No unique bean of type [org.springframework.security.authentication.AuthenticationManager] is defined:


      Click here to attend Spring Framework 4.x and certification course with a discount

If you get the following exception while injecting AuthenticationManager object inside services/controller classes using Spring Framework.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.security.authentication.AuthenticationManager] is defined: expected single matching bean but found 3: [org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0, org.springframework.security.authentication.ProviderManager#0, org.springframework.security.authenticationManager]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement
.inject(AutowiredAnnotationBeanPostProcessor.java:553)

… 39 more

You should add the Qualifier Annotation for the bean AutoWired;

@Autowired
@Qualifier(“authenticationManager”)
public void setAuthenticationManager(AuthenticationManager autm) {
authenticationManager = autm;
}

I assume you declared alias for the AuthenticationManager object such as
<authentication-manager alias=”authenticationManager“>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s