之前没留意web.xml文件的加载顺序,后面遇到问题再回过头来看,先说我遇到的原因如下,

No qualifying bean of type xxx found for dependency  expected at least 1 bean which qualifies as

这种错误原因很多,我这种是因为配置文件的加载顺序引起的

spring-servlet.xml:中有如下三个部分

1 扫描包

2 注解功能

3 视图解析器

1.我的web.xml中配置

<context-param>
    <param-name>contextConfigLocation</param-name>
       <param-value>  
        /WEB-INF/classes/conf/spring/spring-admin.xml
        </param-value>
   </context-param>

2.spring-admin.xml中配置如下,这里只设置需要进行Spring注解扫描的类包,但是扫包过程中由于controller中使用到了其他类的对象,并且使用注解注入,但是这时候去通过类型或者名字查找该依赖对象时,因为还没有加载到spring-servlet.xml,所以容器中还不存在其它bean对象,自然就会报错误No qualifying bean of type xxx found for dependency  expected at least 1 bean which qualifies as。

<!-- 设置需要进行Spring注解扫描的类包 -->
<context:annotation-config />
<context:component-scan base-package="包名" use-default-filters="false">
 <context:include-filter type="regex" expression=".*.*.controller.*"/>  
</context:component-scan>

3.而我真正扫除controller其它类的对象注入依赖bean的xml在spring-servlet.xml中,放在web.xml中的servlet中

<!-- web.xml中配置的,加载的就是*-servlet.xml文件,当前默认加载的是springMVC-servlet.xml如果不给项目指定加载文件
  ,默认加载的就是<servlet-name>名字</servlet-name>,名字-servlet.xml文件 , contextConfigLocation不是必须的,
   如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->  
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
       classpath*:/conf/spring/spring-servlet.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

4.spring-servlet.xml

<!-- 
   spring-servlet.xml:
1 扫描包

2 注解功能

3 视图解析器
  
    -->
    
       <!-- 扫描注解组件并且自动的注入spring beans中.   
        例如,他会扫描@Controller 和@Service @Component,,@Respotory这些注解的类下的文件.所以确保此base-package设置正确. -->  
    <context:component-scan base-package="xxxxxx" />  
  
    <!-- 配置注解驱动的Spring MVC Controller 的编程模型.注:次标签只在 Servlet MVC工作! -->  
    <mvc:annotation-driven />  
    
      <!-- 激活spring的注解. -->  
    <context:annotation-config />

<!-- 视图解释类 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${base.view.prefix}" />
<property name="suffix" value="${base.view.suffix}" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

所以我们是先扫包注入bean,后面才能使用,和我们变量声明是一个道理。为了验证我特意把controller中其它依赖对象去掉后,果然没有出现上面这个错误。

No qualifying bean of type [xxxxxxxxx.AccountInfoService] found for dependency

在web.xml文件中元素的加载顺序与它们在 web.xml 文件中的先后顺序无关。加载的顺序是:context-param->listener -> filter -> servlet

这篇文章总结的不错,来自http://blog.csdn.net/futudeniaodan/article/details/53187550

一、概述

1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。

2、紧接着,容器创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。

3、容器将<context-param>转换为键值对,并交给servletContext。

4、容器创建<listener>中的类实例,创建监听器

二 、 load-on-startup

load-on-startup 元素在web应用启动的时候指定了servlet被加载的顺序,它的值必须是一个整数。如果它的值是一个负整数或是这个元素不存在,那么容器会在该servlet被调用的时候,加载这个servlet 。如果值是正整数或零,容器在配置的时候就加载并初始化这个servlet,容器必须保证值小的先被加载。如果值相等,容器可以自动选择先加载谁。

在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是:

标记容器是否在启动的时候就加载这个servlet。

当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;

当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。

正数的值越小,启动该servlet的优先级越高。

三  、加载顺序

首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:ServletContext -> listener -> filter -> servlet

同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:context-param -> listener -> filter -> servlet

对于某类配置而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter 配置出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。

servlet 同 filter 类似,此处不再赘述。

由此,可以看出,web.xml 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。

四 、 web.xml文件详解

我将自己知道的web.xml的元素整理了一下:

1、web.xml首先是肯定要包含它的schema.

[xml] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">
  6. </web-app>

其它的元素都放在<web-app></web-app>之中。

<discription></discription> 是对站台的描述
<display-name></display-name> 定义站台的名称
<distributable/> 是指定该站台是否可分布式处理
<context-param></context-param> 用来设定web站台的环境参数,它包含两个子元素:
<param-name></param-name> 用来指定参数的名称
<param-value></param-value> 用来设定参数值

比如:

[xml] view plain copy
  1. <context-param>
  2. <param-name>my_param</param-name>
  3. <param-value>hello</param-value>
  4. </context-param>

在此设定的参数,可以在servlet中用 getServletContext().getInitParameter(“my_param”) 来取得

mime-mapping

<mime-mapping></mime-mapping> 定义某一个扩展名和某一个MIME Type做对映,包含两个子元素:
<extension></extension> 扩展名的名称
<mime-type></mime-type> MIME格式

比如:

[xml] view plain copy
  1. <mime-mapping>
  2. <extension>doc</extension>
  3. <mime-type>application/vnd.ms-word</mime-type>
  4. </mime-mapping>
  5. <mime-mapping>
  6. <extension>xls</extension>
  7. <mime-type>application/vnd.ms-excel</mime-type>
  8. </mime-mapping>

welcom-file-list

<welcome-file-list></welcom-file-list> 用来定义首页的列单,包含一个子元素:
<welcome-file></welcome-file> 指定首页的文件名称

比如:

[xml] view plain copy
  1. <welcome-file-list>
  2. <welcome-file>index.jsp</welcome-file>
  3. <welcome-file>index.html</welcome-file>
  4. </welcom-file-list>

error-page

<error-page></error-page> 用来处理错误代码或异常的页面,有三个子元素:
<error-code></error-code> 指定错误代码
<exception-type></exception-type> 指定一个JAVA异常类型
<location></location> 指定在web站台内的相关资源路径

比如:

[xml] view plain copy
  1. <error-page>
  2. <error-code>404</error-code>
  3. <location>/error404.jsp</location>
  4. </error-page>
  5. <error-page>
  6. <exception-type>java.lang.Exception</exception-type>
  7. <location>/exception.jsp</location>
  8. </error-page>

taglib

<taglib></taglib> 用来设定JSP网页所用到的Tag Library路径,有两个子元素:
<taglib-uri></taglib-uri> 定义TLD文件的URI,在JSP网页中用taglib指令便可取得该URI的TLD文件
<taglib-location></taglib-location> 指定TLD文件相对于web站台的存放位置

比如:

[xml] view plain copy
  1. <taglib>
  2. <taglib-uri>myTaglib</taglib-uri>
  3. <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
  4. </taglib>

resource-ref
<resource-ref></resource-ref> 定义利用JNDI取得站台可利用的资源,有五个子元素:
<description></description> 资源说明
<rec-ref-name></rec-ref-name> 资源名称
<res-type></res-type> 资源种类
<res-auth></res-auth> 资源经由Application或Container来许可
<res-sharing-scope></res-sharing-scope> 资源是否可以共享,有Shareable和Unshareable两个                                                                     值,默认为Shareable

比如,配置数据库连接池就可在此配置:

[xml] view plain copy
  1. <span style=""><resource-ref>
  2. <description>JNDI JDBC DataSource of shop</description>
  3. <res-ref-name>jdbc/sample_db</res-ref-name>
  4. <res-type>javax.sql.DataSource</res-type>
  5. <res-auth>Container</res-auth>
  6. </resource-ref></span><span style=""><span style="line-height:24px"> </span></span>

jsp-config
<jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素。
其中<taglib>元素在JSP 1.2时就已经存在;而<jsp-property-group>是JSP 2.0 新增的元素。
<jsp-property-group>元素主要有八个子元素,它们分别为:
1).<description>:设定的说明;
2).<display-name>:设定名称;
3).<url-pattern>:设定值所影响的范围,如:/CH2 或 /*.jsp;
4).<el-ignored>:若为true,表示不支持EL 语法;
5).<scripting-invalid>:若为true,表示不支持<% scripting %>语法;
6).<page-encoding>:设定JSP 网页的编码;
7).<include-prelude>:设置JSP 网页的抬头,扩展名为.jspf;
8).<include-coda>:设置JSP 网页的结尾,扩展名为.jspf。
一个简单的<jsp-config>元素完整配置:

[xml] view plain copy
  1. <jsp-config>
  2. <taglib>
  3. <taglib-uri>Taglib</taglib-uri>
  4. <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
  5. </taglib>
  6. <jsp-property-group>
  7. <description>Special property group for JSP Configuration JSP example.</description>
  8. <display-name>JSPConfiguration</display-name>
  9. <url-pattern>/jsp/* </url-pattern>
  10. <el-ignored>true</el-ignored>
  11. <page-encoding>GB2312</page-encoding>
  12. <scripting-invalid>true</scripting-invalid>
  13. <include-prelude>/include/prelude.jspf</include-prelude>
  14. <include-coda>/include/coda.jspf</include-coda>
  15. </jsp-property-group>
  16. </jsp-config>

配置web.xml来限制对某些servlet的请求

有时我们只希望通过认证的用户才能请求某些servlet的话,就可以在web.xml中来进行相应的配置,来达到此目的。

这就要用到<security-constraint></security-constraint>元素。
对于tomcat,中web.xml使用security-constraint元素需要在位于<Tomcat-installation-directory>/conf/tomcat-users.xml的XML文件中创建用户名和密码。比如下面的这个tomcat-users.xml文件:

[xml] view plain copy
  1. <?xml version=’1.0′ encoding=’utf-8′?>
  2. <tomcat-users>
  3. <role rolename=”tomcat”/>
  4. <role rolename=”manager”/>
  5. <role rolename=”admin”/>
  6. <user username=”tomcat” password=”tomcat” roles=”tomcat”/>
  7. <user username=”both” password=”tomcat” roles=”tomcat,manager”/>
  8. <user username=”admin” password=”admin” roles=”admin”/>
  9. </tomcat-users>

此XML片段包括一个tomcat-users根元素,它包含一个或多个role和user元素。

然后在Web应用程序的web.xml中创建security-constraint、login-config和security-role元素。

[xml] view plain copy
  1. <security-constraint>
  2. <web-resource-collection>
  3. <web-resource-name>HelloServlet</web-resource-name>
  4. <url-pattern>/HelloServlet</url-pattern>
  5. <http-method>GET</http-method>
  6. <http-method>POST</http-method>
  7. </web-resource-collection>
  8. <auth-constraint>
  9. <description>This applies only to the “tomcat” security role</description>
  10. <role-name>admin</role-name>
  11. </auth-constraint>
  12. <user-data-constraint>
  13. <transport-guarantee>NONE</transport-guarantee>
  14. </user-data-constraint>
  15. </security-constraint>
  16. <login-config>
  17. <auth-method>BASIC</auth-method>
  18. </login-config>
  19. <security-role>
  20. <role-name>admin</role-name>
  21. </security-role>

其中security-constraint元素包含一个或多个web-resource-collection元素,它是描述Web应用程序中的哪些web资源受到指定安全限制的保护。http-method元素指定安全限制覆盖的HTTP方法。上面的例子中,当我们对/HelloServlet的GET或POST请求时将触发配置的安全机制。

auth-constraint元素用于描述允许访问Web组件的安全角色。此例中安全角色的例子有tomcat、manager、admin。而只有当作为admin角色的用户才可以访问HelloServlet。

Web应用程序通过login-config元素来认证用户,并确认该用户是否为正确的角色。
longin-config包含的transport-guarantee子元素用来指定认证方法,BASIC是一种常见的Web认证方式,浏览器给用户提示一个对话框,要求输入用户名和密码,随后Tomcat将给出的用户名和密码与tomcat-users.xml中的用户名和密码进行比较,然后使用前面的security-constraint配置来确定用户是否可访问受保护的servlet。

(除BASIC外,还可以是FORM、CLIENT-CERT、DIGEST等)

其实这种认证方法实际上有两个步骤:
1、检查提供的用户名和密码是否正确。
2、判断用户是否映射到特定的安全角色。例如,用户可能提供了正确的用户名和密码,但没有映射到特定的安全角色,也将被禁止访问特定的Web资源。

No qualifying bean of type xxx found for dependency expected at least 1 bean which qualifies as ...相关推荐

  1. No qualifying bean of type ‘javax.servlet.Filter‘ available: expected at least 1 bean which qualif

    No qualifying bean of type 'javax.servlet.Filter' available: expected at least 1 bean which qualifie ...

  2. No qualifying bean of type ‘java.lang.String‘ available: expected at least 1 bean which qualifies

    一.问题复现和详细异常 今天一个同事在开发时给一个类(AServiceImpl)加了@AllArgsConstructor,希望通过private final 的方式将XxClass 和 XxxCla ...

  3. No qualifying bean of type [XXX.XXX.XXX] found for dependency: expected at least 1 bean which q

    老生常谈的问题,没写spring bean的注解(@service,@Repository..),所以无法完成自动装配.如果注解不行就在xml里面试试.

  4. 【无标题】No qualifying bean of type ‘javax.sql.DataSource‘ available: expected at least 1

    No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifie ...

  5. No qualifying bean of type ‘java.lang.String‘ available expected

    错误提示: java.lang.IllegalStateException: Failed to load ApplicationContextat org.springframework.test. ...

  6. No qualifying bean of type xxx‘ available 的一种解决方法

    No qualifying bean of type xxx' available 的一种解决方法 参考文章: (1)No qualifying bean of type xxx' available ...

  7. 毕业设计Spring boot问题记录(后端二):No qualifying bean of type “xxx“: expected at least 1 bean which qualifies…

    报错信息:No qualifying bean of type "xxx" idea Disconnected from the target VM, address: '127. ...

  8. 解决——》No qualifying bean of type ‘XXX‘ available

    推荐链接:     总结-->[Java]     总结-->[Mysql]     总结-->[Spring]     总结-->[SpringBoot] 解决-->N ...

  9. springboot使用junit单元测试是发生报错 Field taskUtils in com.xxx.xxx.xxxx.xxx required a bean of type 'xxx.xxx.

    使用junit单元测试mybatis时发生如下错误 Error starting ApplicationContext. To display the conditions report re-run ...

最新文章

  1. hdf heg 批量拼接_[转载]MODIS Aerosol product/MODIS气溶胶产品
  2. SAP QM 物料主数据检验类型上的Skips allowed
  3. 软考程序员2009年下午试题是否有误?
  4. andorid手机电脑操作
  5. HDOJ 2049 不容易系列之(4)——考新郎
  6. 王爽汇编第九章学习笔记
  7. STL - bitset
  8. [pytorch、学习] - 4.1 模型构造
  9. JAVA-重写equalse规范、技巧
  10. 【学习笔记】局域网基本概念和体系结构,以太网、无线局域网与PPP协议、HDLC协议
  11. 作为一名程序员,他们工作时的快乐来源于哪里?不仅仅是高工资!
  12. IE6,7下实现white-space:pre-wrap;
  13. python datetime to timestamp_python timestamp和datetime之间转换详解
  14. 关于csv文件转化为张量
  15. SQLServer 批量插入数据的两种方法
  16. sql2005安装过程,(不装C盘)
  17. 如何将kux格式的视频转换成我们常用的MP4格式
  18. DeepStream初步学习
  19. 文字识别方法全面整理
  20. 马云开酒吧了,酒吧叫“平头哥”,真猛~

热门文章

  1. python画正方形并涂色_这种图片怎么用python画出来,每一个数字代表一种颜色?...
  2. mysql server安装报错_安装VtigerCRM报错:MySQL Server should be configured with
  3. pycharm中出现pytest_pytest文档3-pycharm运行pytest
  4. Rhodamine-PEG-NH2,罗丹明-聚乙二醇-氨基的结构式,一文了解RB-PEG-NH2的使用
  5. 哥德尔不完备定理”到底说了些什么?
  6. FFmpeg将mp4转成flv
  7. H5移动端实现手机震动效果
  8. 【树莓派C语言开发】实验14:PS2游戏手柄模块(关联PCF8591)
  9. python:超级画板
  10. Get传递参数时发生java.lang.IllegalArgumentException异常