相关文章:
ContextLoaderListener和DispatcherServlet区别(contextConfigLocation、contextClass参数)&父子容器 web.xml 包含详细的父子容器概念
Spring MVC的web.xml配置详解 完整详细的配置,其中 ContextLoaderListener和DispatcherServlet 分别对应父子容器
spring容器和springmvc容器,以及web容器(tomcat是具体的一种实现)的关系
Apache和Tomcat区别 & WEB服务器(容器)、应用程序服务器、HTTP服务器区别
【Tomcat】Web容器是什么?Tomcat 就是一个“HTTP 服务器 + Servlet 容器”的Web容器
spring容器和springmvc容器
结论:
-
Spring容器 Spring容器是管理
service
和dao
的。 -
SpringMVC容器 SpringMVC容器是管理
controller
对象的。
说到spring和springmvc,其实有很多工作好多年的人也分不清他们有什么区别,如果你问他项目里用的什么MVC技术,他会说我们用的spring和mybatis,或者spring和hibernate。
在潜意识里会认为springmvc就是spring,之前我也是这么认为的,哈哈。
虽然springMVC和spring有必然的联系,但是他们的区别也是有的。下面我就简单描述下
首先 springmvc和spring它俩都是容器,容器就是管理对象的地方,例如Tomcat(web容器的一种实现),就是管理servlet对象的,而springMVC容器和spring容器,就是管理bean对象的地方
,再说的直白点,springmvc就是管理controller对象的容器,spring就是管理service和dao的容器,这下你明白了吧。所以我们在springmvc的配置文件里配置的扫描路径就是controller的路径
,而spring的配置文件里自然配的就是service和dao的路径
什么是servlet对象,参见下一个章节
springmvc的配置文件 spring-mvc.xml示例:
<context:component-scan base-package="com.smart.controller" />
spring的配置文件 applicationContext-service.xml示例:
<!-- 扫描包加载Service实现类 -->
<context:component-scan base-package="com.smart.service"></context:component-scan>
或者
<context:component-scan base-package="com.smart">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
至于他是怎么管理起来的,又是怎么注入属性的,这就涉及到他们底层的实现技术了
其次, spring容器和springmvc容器的关系是父子容器的关系。spring容器是父容器,springmvc是子容器。在子容器里可以访问父容器里的对象,但是在父容器里不可以访问子容器的对象,说的通俗点就是,在controller里可以访问service对象,但是在service里不可以访问controller对象
父子容器概念 参见 ContextLoaderListener和DispatcherServlet区别(contextConfigLocation、contextClass参数)&父子容器 web.xml
所以这么看的话,所有的bean,都是被spring或者springmvc容器管理的,他们可以直接注入。然后springMVC的拦截器也是springmvc容器管理的,所以在springmvc的拦截器里,可以直接注入bean对象
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/employee/**" ></mvc:mapping>
<bean class="com.smart.core.shiro.LoginInterceptor" ></bean>
</mvc:interceptor>
</mvc:interceptors>
web容器
详情参见Apache和Tomcat区别 & WEB服务器(容器)、应用程序服务器、HTTP服务器区别
而web容器又是什么鬼?
web容器(web服务器)主要有:Apache、IIS、Tomcat、Jetty、JBoss、webLogic等,而Tomcat、Jetty、JBoss、webLogic同时也是servlet容器,或者说他们还包含了servlet容器。没有servlet容器,你也可以用web容器直接访问静态页面,比如安装一个apache等,但是如果要显示jsp/servlet,你就要安装一个servlet容器了,但是光有servlet容器是不够的,因为它要被解析成html输出,所以你仍需要一个web容器。大多数servlet容器同时提供了web容器的功能,也就是说大多servelt容器可以独立运行你的web应用。
web容器是管理servlet,以及监听器(Listener)和过滤器(Filter)的
。这些都是在web容器的掌控范围里。但他们不在spring和springmvc的掌控范围里
。因此,我们无法在这些类中直接使用Spring注解的方式来注入我们需要的对象,是无效的,web容器是无法识别的。
我们有时候又确实会有这样的需求,比如在容器启动的时候,做一些验证或者初始化操作,这时可能会在监听器里用到bean对象;又或者需要定义一个过滤器做一些拦截操作,也可能会用到bean对象。
那么在这些地方怎么获取spring的bean对象呢?下面我提供两个方法:
方法一:
public void contextInitialized(ServletContextEvent sce) {
ApplicationContext context = (ApplicationContext) sce.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
UserService userService = (UserService) context.getBean("userService");
}
方法二:
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
UserService userService = (UserService) webApplicationContext.getBean("userService");
}
注意:以上代码有一个前提,那就是servlet容器在实例化ConfigListener
并调用其方法之前,要确保spring容器已经初始化完毕!而spring容器的初始化也是由Listener(ContextLoaderListener
)完成,因此只需在web.xml中先配置初始化spring容器的Listener,然后再配置自己的Listener。