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

时间:2016-06-14 09:53:41

标签: java spring hibernate spring-mvc

我不知道为什么在我启动弹簧应用程序时反复出现此错误。这是错误的堆栈跟踪

 SEVERE: Exception starting filter springSecurityFilterChain
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1174)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:201)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051)
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:199)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4574)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5193)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这是我的web.xml文件,我在其中定义了springsecurityfilterchain

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
              org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在名为appconfig-security.xml的文件中,我有这个配置

<http auto-config="true">
        <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password"/>
        <logout logout-success-url="/login?logout" />
    </http>

已添加更多信息

这是我的appconfig-mvc.xml,其中包含internalviewresolver和其他类型配置的定义

<!-- DataSource definition -->
    <import resource="appconfig-data.xml" />


    <!-- added this line of code -->
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <!-- ends here -->


    <!-- Mapping Controllers -->
    <context:annotation-config />

    <context:component-scan base-package="com.springfoundation" />

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:validation</value>
            </list>
        </property>
    </bean>

<!-- removed internal view resolver from this file -->
<!-- JSP Templates and Pages -->
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

这是我的appconf-data.xml文件,其中包含数据库连接等配置

<!-- Configure the data source bean -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- Configure the entity manager factory bean -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.springfoundation.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Configure the transaction manager bean -->
    <bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- Enable annotation driven transaction management -->
    <tx:annotation-driven/>

    <!--
      Configure Spring Data JPA and set the base package of the
      repository interfaces
    -->

    <jpa:repositories base-package="com.springfoundation.repository"/>

这就是我将我在资源下的spring文件夹中定义的配置xml文件导入我的web.xml文件,并在我的web.xml文件中显示完整内容的方法

 <display-name>Spring MVC Application</display-name>
    <description>This is a template for a simple and informational Login and CRUD WebAPP.</description>

    <!-- - Location of the XML file that defines the root application context. 
         - Applied by ContextLoaderListener. -->


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/appconfig-mvc.xml, 
            classpath:spring/appconfig-security.xml, 
            classpath:spring/appconfig-root.xml
        </param-value>
    </context-param>


<!-- 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/appconfig-root.xml
        </param-value>
    </context-param> -->
<!-- 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/main-dispatcher-servlet.xml, 
            classpath:spring/spring-security-config.xml
        </param-value>
    </context-param>
-->




    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Servlet that dispatch request from Controllers -->
<!--  
    <servlet>
        <servlet-name>springSimple</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    -->

    <servlet>
        <servlet-name>springSimple</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/appconfig-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springSimple</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>



    <!-- Spring Security -->

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
                  org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- General Configurations -->

    <welcome-file-list>
        <!-- First Page load -->
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <!-- No found page -->
        <error-code>404</error-code>
        <location>/WEB-INF/jsp/errors/404.jsp</location>
    </error-page>

我不知道为什么我一直收到这个错误。

3 个答案:

答案 0 :(得分:0)

请在applicationContext.xml中添加bean定义,如下所示。

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>

答案 1 :(得分:0)

尝试使用springSecurityFilterChain的以下配置。它对我有用。请注意contextAttribute

<init-param>
    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
     <init-param>
        <param-name>contextAttribute</param-name>
        <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.appServlet</param-value>
    </init-param>
</filter>

答案 2 :(得分:0)

我认为你在applicationContext.xml中没有bean定义,如下面的

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy">  
</bean>

ApplicationContext是一个xml文件,它是所有bean及其定义的容器。名称可以是applicationContext.xml以外的任何名称。在您的情况下,它可能是appconfig-mvc.xml或appconfig-data.xml。尝试发布这两个xml文件

注意:如果获得NoSuchBeanDefinitionException,则表示您尚未创建bean定义。
谢谢!