org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'authenticationManager' is defined

StackOverflow https://stackoverflow.com/questions/22183309

Question

I use spring security with JSF.

my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>PrimeFaces Web Application</display-name>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <!-- Spring Context Configuration' s Path definition -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
         </param-value>
    </context-param>


    <!-- welcome file -->
    <welcome-file-list>
        <!-- <welcome-file>faces/index2.xhtml</welcome-file> -->
        <welcome-file>index2.xhtml</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>bootstrap</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>



    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
    </context-param>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

    <!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. 
        It is registered to Servlet Container -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
</web-app>

My spring-security.xml file:

<?xml version="1.0" encoding="UTF-8"?>

xmlns:sec="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<sec:http auto-config="true" use-expressions="true">
    <sec:intercept-url pattern="/home2" access="hasRole('ROLE_USER')" />

    <sec:intercept-url pattern="/**" access="permitAll"/>
    <sec:form-login login-page="/index2.xhtml"/>
</sec:http>

<sec:authentication-manager alias="authenticationManager">                             
    <sec:authentication-provider>
        <sec:user-service>
            <sec:user authorities="ROLE_USER" name="admin" password="admin" />
        </sec:user-service>
    </sec:authentication-provider>
</sec:authentication-manager>

My ApplicationContext.xml file:

<bean id="viewmb" class="com.infpaymoney.managedbeans.ViewManagedBean" scope="prototype">
    <property name="authenticationManager" ref="authenticationManager"></property>
</bean>

And my managed bean:

@Component
@ManagedBean(name = "viewmb")
@RequestScoped
public class ViewManagedBean implements Serializable {
......
@ManagedProperty(value="#{authenticationManager}")
private AuthenticationManager authenticationManager = null;
public AuthenticationManager getAuthenticationManager() {
    return authenticationManager;
}

public void setAuthenticationManager(AuthenticationManager authenticationManager) {
    this.authenticationManager = authenticationManager;
}
.....

but when i run my applocation i have the folowing error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewmb' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'authenticationManager' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'authenticationManager' is defined
at ......
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'authenticationManager' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 50 more

The authenticationManager is well defined on spring-security file, and i don't know why is not visible bye the applicationContext.xml file

Was it helpful?

Solution

Add the spring-security.xml file to your context-param or import it in the application context.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml,
        /WEB-INF/spring-security.xml
     </param-value>
</context-param>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top