问题描述:

web项目启动失败,查看日志发现报错:No bean named ‘springSecurityFilterChain’ available
注意:我的springsecurity是以配置文件的方式配置的,日志报错的关键信息如下

org.springframework.beans.factory.NoSuchBeanDefinitionException:No bean named 'springSecurityFilterChain' available

翻译一下,就是Spring的IOC容器中没有springSecurityFilterChain这个组件


原因分析:

原因是我们的springsecurity是以配置文件没有被加入到Spring的IOC容器所导致的,即使加入到了SpringMVC的IOC容器也不行;

ContextLoaderListener负责了Spring IOC容器的初始化,主要扫描的类是XxxxService、XxxxMapper类等。DispatcherServlet负责了SpringMVC的IOC容器的初始化,主要负责XxxxHandler、view-controller。而在项目中为了让Spring Security可以针对浏览器的请求进行权限控制,所以需要让Spring或SpringMVC来扫描WebAppSecurityConfig配置类。但是在项目启动过程中报错未找到bean,和三大组件的启动顺序相关:

1 ContextLoaderListener初始化,创建Spring的IOC容器

2 DelegatingFilterProxy初始化,查找IOC容器,查找bean

3 DispatcherServlet初始化,创建SpringMVC的IOC容器

也就是,我们一开始将这个bean加入了SpringMVC容器,但还没来的及加载就报错了,因为这种情况下要在第三步(DispatcherServlet初始化时)才能将springSecurityFilterChain加入到容器中,但组件检查过程在第二步就做完了,此时毕然检查不到springSecurityFilterChain组件,程序终止运行并报错

如下图分析


解决方案:

这是我的项目结构

方法一:将这个bean加入Spring的IOC容器(推荐)

spring负责扫描除了springmvc单独负责的三类@Controller外的全部组件,也就是将springSecurityFilterChain这个Filter所需的bean加入到Spring IOC容器中。

Spring扫描除了三类Controller外的所有组件

<context:component-scan base-package="com.wzh.atcrowdfunding"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/><context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/></context:component-scan>

只是让SpringMVC扫描关于@Controller的组件

    <context:component-scan base-package="com.wzh.atcrowdfunding" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/></context:component-scan>

方法二:把两个IOC容器的加载过程合二为一(不推荐)

思路是不使用contextConfigLocation单独加载Spring配置文件,而是直接使用;springDispatcherServlet加载Spring的SpringMVC的配置文件,这样那个bean无论在Spring的IOC还是SpringMVC的IOC都可以;
之所以不推荐,原理和方法三修改源码的办法类似,会破坏程序结构;

注意:AdminServiceImpl中需要使用passwordEncoder组件,由于springsecurity相关组件都扫描进了SpringMVC的IOC容器,但这是在servcie层,service层都是由Spring负责的,也就passwordEncoder最后会被装载到Spring容器,这样会显示当前没有可用的bean

  <!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><!--   一口气加载spring-web-mvc.xml,spring-persist-mybatis.xml,spring-persist-tx.xml这三个配置文件 --><param-value>classpath:spring-*.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>*.html</url-pattern><url-pattern>*.json</url-pattern></servlet-mapping>

方法三:修改源码(极力反对)

Error:No-bean-named-springSecurityFilterChain-available相关推荐

  1. A component required a bean named 'XXXService' ...

    报错如下: Description: A component required a bean named 'checkPsptService' that could not be found. Act ...

  2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  3. SpringMVC莫名其妙出现No bean named 'cacheManager' is defined错误

    2019独角兽企业重金招聘Python工程师标准>>> 在使用Interiij idea创建SpringMVC项目时,莫名其妙出现了No bean named 'cacheManag ...

  4. bean named 'entityManagerFactory' that could not be found错误及解决

    问题: 在Eclipse下报如下错误:bean named 'entityManagerFactory' that could not be found. 发生场景: 导入JPA依赖包后, 在Serv ...

  5. NoSuchBeanDefinitionException: No bean named 'tDubboServiceImpl' is defined 的解决办法

    问题概述 关于这儿问题是在微服务开发过程中遇到的,通过本地远程调用dubbo接口,用xml实例化bean时报 " No bean named 'tDubboServiceImpl' is d ...

  6. Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dao' is exp

    异常信息: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with nam ...

  7. Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named ‘bookService‘

    1 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'c ...

  8. Error creating bean with name 'eurekaClientConfigBean': Singleton bean creation not allowed!

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 今天发现一个错误,简单记录一下,运行一个项目一直启动不了,发现控制台报错了. 首先说明一下这是一个Spring boot ...

  9. Bean named 'rateBoardService' is expected to be of but was actually of type 'com.sun.proxy.$Proxy69'

    Bean named 'rateBoardService' is expected to be of type 'xxxx' 今天在写一个测试用例的时候遇到个Bug,提示语如下: 可以只看第一行 or ...

  10. Bean named ‘xxx‘ is expected to be of type ‘xxx‘ but was actually of type ‘zzzz‘

    Bean named 'whiteListConfig' is expected to be of type 'java.lang.String' but was actually of type ' ...

最新文章

  1. JDBC学习笔记02【ResultSet类详解、JDBC登录案例练习、PreparedStatement类详解】
  2. python文件处理系列(二):Excel文件读取库xlwings
  3. 从选秀策略看市场测试
  4. 图书馆管理系统用户端心得
  5. springBoot修改代码不需要重启-热部署
  6. 详解SaaS产品的5类核心指标
  7. android adb移植到arm,android-ndk – 为arm处理器构建android adb
  8. delphi 判断两个时间差是否在一个指定范围内
  9. Java基础篇:如何使用instanceof
  10. 有佳文档管理v2.03
  11. 计算机网络系统集成实验指导,系统集成实验指导.docx
  12. 【深度学习】 图像识别实战 102鲜花分类(flower 102)实战案例
  13. VMware Workstation 不可恢复错误: (vmx)Exception 0xc0000006 (disk error while paging) has occurred.
  14. 数据库概述09(数据库中的锁机制)
  15. MIT-BIH介绍(三)如何使用MIT-BIH?
  16. Win7系统的家庭版,旗舰版和专业版的区别
  17. java工单管理系统_java毕业设计_springboot框架的工单管理系统
  18. 答应我!忘了他吧!不要再用print了,以后调试Python用冰淇淋
  19. Python数据爬取之0基础小白实战(三)源码解析
  20. uddi java_使用UDDI4J连接JUDDI

热门文章

  1. Java == equal
  2. 【转】新.Net开发必备工具详解之Snippet Compiler。
  3. [HTTP] Nginx代理以及面向未来的HTTP
  4. panda 函数笔记(merge\DataFrame用法\DataFrame.plot)
  5. javadoc 程序包android.content不存在,Eclipse中的Javadoc无法识别包
  6. mysql zpi版的如何配置_Mysql zip版 安装配置
  7. 输出斐波那契数列不大于1000的序列
  8. MINST数据集下载失败
  9. jquery.treeview.js php mysql,jquery.treeview应用
  10. mysql的ps.setmaxrows_mysql自定义函数实现表的指定列进行数据脱敏(PS:来自mysql小白的提问)...