美食分享网站建设策划书网站搭建介绍

当前位置: 首页 > news >正文

美食分享网站建设策划书,网站搭建介绍,app源码开发公司,阿里巴巴网站运营这一节我们来回答上篇文章中避而不谈的有关什么是RootApplicationContext的问题。 这就需要引入Spring MVC的有关Context Hierarchy的问题。Context Hierarchy意思就是Context层级#xff0c;既然说到Context层级#xff0c;说明在Spring MVC项目中#xff0c;可能存在不止…这一节我们来回答上篇文章中避而不谈的有关什么是RootApplicationContext的问题。 这就需要引入Spring MVC的有关Context Hierarchy的问题。Context Hierarchy意思就是Context层级既然说到Context层级说明在Spring MVC项目中可能存在不止一个Context。 Context是上下文的意思我们可以直接理解为容器上一篇文章我们提到了ServletApplicationContext的概念可以理解为Servlet容器。 除此之外Spring项目中肯定还有有IoC容器我们今天就来聊一下这两个容器之间的关系。 为什么需要容器 我们可以这样理解凡是具有生命周期的对象也就是说对象并不是在当前现成创建、使用完成之后就销毁的而是在当前现成使用完成之后不销毁、其他线程还需要继续使用的。这中对象就需要一个容器来保存。 比如Spring的单例Bean在Spring初始化的过程中就会创建并存入Ioc容器之后应用使用过程中从Ioc容器中获取。 Servlet对象比如DispatchServlet也是这样在Web应用初始化的过程中创建Controller、ViewResolver、ExceptionHandler等对象随之也完成创建这些对象在整个应用的生命周期中会反复使用因此Servlet也必须要有一个容器来存储。 Spring把容器称之为上下文Context。 我们可以把Servlet容器叫做WebApplicationContext因为Servlet的出现就是为了解决Web应用的所以自然而然的我们可以把Servlet容器称为WebApplicationContext。 相对应的Spring Ioc容器我们可以称之为RootApplicationContext根容器。 Servlet和根容器的关系 下图一目了然的说明了两者之间的关系
Servlet容器存放Controller、VIewResolver、HanderMapping等DispatcherServlet的相关对象根容器可以存放其他Service、Repositories等对象。 一个DispatcherServlet可以对应的有一个Servlet容器一个Web应用可以有多个DispatcherServlet这种应用其实比较少见所以一个应用可以有多个Servlet容器。但是一般情况下即使有多个Servlet容器一个应用也希望只有一个根容器以便在不同的Servlet容器之间共享根容器的对象。 举例 我们下面用几个例子来说明两者之间的关系。 还是延用上一篇文章的例子并做如下简单的改造。 首先增加一个单例bean以便启用Spring IoC容器。我们只是简单引入IoC容器单例bean不需要太复杂 package org.example.service;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component;Component public class UserService {AutowiredApplicationContext app;public String getUserInfo(){System.out.println( application in UserService: app);return This is userinfo…from UserService…;} }只有一个方法返回String。不过为了能说明当前单例Bean所处的容器我们通过Autowired引入ApplicationContext对象希望大家还记得这一点我们前面讲过通过什么样的方式能够在应用中拿到Bean所处的Application对象这样的话我们就能够知道当前Spring应用的Ioc容器具体是哪个对象。 其次我们需要新增一个Spring Ioc容器的配置类我们称之为RootConfiguration配置类仅指定扫描路径即可 import org.springframework.context.annotation.Configuration;Configuration ComponentScan(org.example.service) public class RootConfiguration { }最后Controller改造一下与UserService一样引入ApplicationContextController中的ApplicationContext我们可以人为他就是Servlet容器log打印一下具体的Context同时我们打印一下当前Servlet容器的父容器 package org.example.controller;import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.ModelAndView;Controller public class HelloWorldController {AutowiredUserService userService;AutowiredApplicationContext app;GetMapping(/hello)ResponseBodypublic String hello(ModelAndView model){DispatcherServlet d;String userInfo userService.getUserInfo();System.out.println(app in controller:app);System.out.println(servletContexts parent Contextapp.getParent());return h1userInfo/h1;} }OK准备工作完成开始验证。 举例1 根容器不是必须存在 首先根容器不是必须存在的。 但是由于我们增加了UserService这个bean所以必须有Ioc容器我们必须为IoC容器指定配置文件的包扫描路径。 既然我们说根容器不是必须存在那么意思就是说Servlet容器要同时充当IoC容器的角色。 所以我们必须在MvcConfiguration中增加宝扫描路径 package org.example.configuration;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;Configuration ComponentScan({org.example.service,org.example.controller}) //ComponentScan({org.example.controller}) public class MvcConfiguration { }MvcInitializer中的getRootConfigClasses()返回null则应用初始化的过程中就不会创建根容器通过查看createRootApplicationContext方法源码得出的结论 public class MvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return null;}启动应用测试结果 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XBftrdeQ-1693234193599)(/img/bVc9m07)] 说明应用是可以正常运行的后台log application in UserService:WebApplicationContext for namespace dispatcher-servlet, started on Tue Aug 22 22:35:34 CST 2023 app in controller:WebApplicationContext for namespace dispatcher-servlet, started on Tue Aug 22 22:35:34 CST 2023 servletContexts parent Contextnull后台log说明Servlet容器和Spring IoC容器是同一个他们的父容器是null。 指定Ioc容器为父容器 首先修改MvcConfiguration指定Servlet容器只扫描Controller包 package org.example.configuration;import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;Configuration ComponentScan({org.example.controller}) public class MvcConfiguration { }MvcInitializer中的getRootConfigClasses()返回我们新增的RootConfiguration则应用初始化的过程中就会创建根容器 public class MvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return new Class[] {RootConfiguration.class};}启动应用测试 application in UserService:Root WebApplicationContext, started on Tue Aug 22 22:44:08 CST 2023 app in controller:WebApplicationContext for namespace dispatcher-servlet, started on Tue Aug 22 22:44:09 CST 2023, parent: Root WebApplicationContext servletContexts parent ContextRoot WebApplicationContext, started on Tue Aug 22 22:44:08 CST 2023有测试结果可知 1. UserService所处的是根容器。 2. Controller所处的是Servlet容器。 3. Servlet容器的父容器是根容器。 测试结果验证了我们前面的推论而且Spring底层自动将Servlet容器的父容器设置为了根容器在什么地方设置的 Spring MVC框架在DispatcherServlet的初始化过程中init方法initWebApplicationContext的时候设置ServletContext的父容器为根容器。 上一篇 Spring MVC 三 基于注解配置