通常springMVC和Spring的整合过程当中,咱们喜欢配置两个文件application-*.xml和springmvc-.xml,可是配置很差的话,很容易出现这样的问题:Injection of resource dependencies failed; nested exception is org.springfra......,也就是注入bean失败的问题。web
通常对文件的配置喜欢<context:component-scan base-package="com.edu"/>这样的话,能够扫描整个包下面的文件,可是也会致使效率的问题,由于是扫描这个包下面全部的controller、service和bean等。另一种方法是,扫描的具体的包,一个一个配置,可是这样有很繁琐。spring
因此咱们会采用这样的配置:express
<context:component-scan base-package="com.edu" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>mvc
利用use-default-filters和context:include-filter属性来进行区别,这样的话就能够根据本身的须要来扫描那个层,是controller仍是service层。可是这样话,有些类就可能注入不进来,致使出现这样的问题:Injection of resource dependencies failed; nested exception is org.springfra......app
2.解决方案component
由于上面的问题,因此咱们须要对区别application-*.xml和springmvc-.xml非常了解。通常我喜欢把controller层springmvc-.xml文件中,service层配置在application-*.xml中。xml
因此springmvc-.xml也是处理controller层相关的注解,而application-*.xml通常配置其余,好比出c3po0等。可是也会致使出现:Injection of resource dependencies failed; nested exception is org.springfra......,因此通常状况下我会把<mvc:annotation-driven ></mvc:annotation>等注解配置在 <context:component-scan />的前面,由于 <context:component-scan /> ,会自动注入@controller等注解。这又涉及到<mvc:annotation-driven >和<context:component-scan />他们两个注解之间的区别。ci
第二个区别就是application-*.xml和springmvc-*.xml的加载顺序是不同的,因此必定分清楚,那些要先加载的,应该放在application-*.xml中。由于这涉及到web.xml的加载顺序问题。application-*.xml配置在<context-param>下,而springmvc-*.xml配置在servlet下面。根据web.xml的加载顺序是:context-param -> listener -> filter -> servlet。也能够说是按照加载顺序来配置文件的。servlet