Springmvc和spring的父子容器

一般我们使用spring和springMVC的时候,web.xml的配置一般先配置加载spring

<!--加载spring容器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

接下来我们配置了springMVC作为前端控制器,这时候,就相当于创建了一个新的容器,springMVC,他们两者之间的关系我们可以看成这样Springmvc和spring的父子容器Springmvc和spring的父子容器Springmvc和spring的父子容器Springmvc和spring的父子容器Springmvc和spring的父子容器Springmvc和spring的父子容器Springmvc和spring的父子容器

这时候,springMVC里面存的就是controller,而spring里面存的就是service和dao,平时我们使用的时候,把dao注入到service里面,他们两个都是在同一个容器里面,当dao初始化的时候它会自动注入,所以没什么问题。当我们把service注入到controller里面的时候也没什么问题,因为子容器可以访问父容器中的对象而父容器不能访问子容器对象,也就是说controller不能注入到service里面的

例如:
在applicationContext-service中配置:
<!-- 扫描包加载Service实现类 -->
<context:component-scan base-package="com"></context:component-scan>
这样会把所有对象都放到了spring中,所以请求发生时会扫描@Controller、@Service、@Repository、@Compnent
但是,在springMVC中没有了对象,所以Springmvc。Xml中不扫描。
结论:springmvc。不能提供服务,因为springmvc子容器中没有controller对象。