org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'authenticationManager'的bean

时间:2014-03-04 21:21:13

标签: java spring jsf spring-security

我在JSF中使用spring security。

我的web.xml文件

<?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>

我的spring-security.xml文件:

<?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>

我的ApplicationContext.xml文件:

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

我的托管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;
}
.....

但是当我运行我的applocation时,我有以下错误

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

authenticationManager在spring-security文件中定义良好,我不知道为什么appContext.xml文件不可见

1 个答案:

答案 0 :(得分:2)

spring-security.xml文件添加到context-param或在应用程序上下文中导入。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml,
        /WEB-INF/spring-security.xml
     </param-value>
</context-param>