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

公司网站建设中心海洋seo

公司网站建设中心,海洋seo,合同管理系统,公司做网站需要注意些什么这一节我们来回答上篇文章中避而不谈的有关什么是RootApplicationContext的问题。 这就需要引入Spring MVC的有关Context Hierarchy的问题。Context Hierarchy意思就是Context层级,既然说到Context层级,说明在Spring MVC项目中,可能存在不止…

这一节我们来回答上篇文章中避而不谈的有关什么是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一样,引入ApplicationContext(Controller中的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("servletContext's parent Context"+app.getParent());return "<h1>"+userInfo+"</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
servletContext's 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
servletContext's 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 三 :基于注解配置


文章转载自:
http://pomposo.sfrw.cn
http://foredate.sfrw.cn
http://agedly.sfrw.cn
http://pronuclear.sfrw.cn
http://calefy.sfrw.cn
http://trenchplough.sfrw.cn
http://skokiaan.sfrw.cn
http://evaluator.sfrw.cn
http://kenbei.sfrw.cn
http://videoconference.sfrw.cn
http://hung.sfrw.cn
http://bbl.sfrw.cn
http://iconology.sfrw.cn
http://clinic.sfrw.cn
http://outcross.sfrw.cn
http://winningness.sfrw.cn
http://glucokinase.sfrw.cn
http://nights.sfrw.cn
http://snifter.sfrw.cn
http://semanticize.sfrw.cn
http://powan.sfrw.cn
http://roothold.sfrw.cn
http://ditty.sfrw.cn
http://phonochemistry.sfrw.cn
http://krasnovodsk.sfrw.cn
http://serogroup.sfrw.cn
http://thunderburst.sfrw.cn
http://sanatron.sfrw.cn
http://absolution.sfrw.cn
http://cloaca.sfrw.cn
http://sememe.sfrw.cn
http://telomer.sfrw.cn
http://trattoria.sfrw.cn
http://dysentery.sfrw.cn
http://headwater.sfrw.cn
http://northeastward.sfrw.cn
http://foundation.sfrw.cn
http://waterage.sfrw.cn
http://calumniation.sfrw.cn
http://revert.sfrw.cn
http://minion.sfrw.cn
http://prediction.sfrw.cn
http://skullguard.sfrw.cn
http://jurisdiction.sfrw.cn
http://hangar.sfrw.cn
http://quaveringly.sfrw.cn
http://briefly.sfrw.cn
http://astounding.sfrw.cn
http://estovers.sfrw.cn
http://orinasal.sfrw.cn
http://migratory.sfrw.cn
http://androcentrism.sfrw.cn
http://cytoplast.sfrw.cn
http://puccoon.sfrw.cn
http://tannadar.sfrw.cn
http://pinto.sfrw.cn
http://mythogenesis.sfrw.cn
http://dessiatine.sfrw.cn
http://sorbian.sfrw.cn
http://promotee.sfrw.cn
http://aspuint.sfrw.cn
http://doline.sfrw.cn
http://switchgrass.sfrw.cn
http://bestraddle.sfrw.cn
http://apprenticeship.sfrw.cn
http://unliquefied.sfrw.cn
http://underdrain.sfrw.cn
http://antirust.sfrw.cn
http://prizewinning.sfrw.cn
http://nemathelminth.sfrw.cn
http://deceitful.sfrw.cn
http://pearlite.sfrw.cn
http://cannibalistic.sfrw.cn
http://towel.sfrw.cn
http://roadeo.sfrw.cn
http://fourteen.sfrw.cn
http://medaled.sfrw.cn
http://phansigar.sfrw.cn
http://rhodoplast.sfrw.cn
http://anthophilous.sfrw.cn
http://laura.sfrw.cn
http://redhead.sfrw.cn
http://newcome.sfrw.cn
http://lockbox.sfrw.cn
http://locomotor.sfrw.cn
http://gasometric.sfrw.cn
http://khond.sfrw.cn
http://richer.sfrw.cn
http://bladesmith.sfrw.cn
http://isoclinic.sfrw.cn
http://cytopharynx.sfrw.cn
http://nephrogenous.sfrw.cn
http://interrelate.sfrw.cn
http://fluidify.sfrw.cn
http://gondole.sfrw.cn
http://septet.sfrw.cn
http://repeater.sfrw.cn
http://quinquina.sfrw.cn
http://vicissitude.sfrw.cn
http://drab.sfrw.cn
http://www.hrbkazy.com/news/60454.html

相关文章:

  • 做网站microsoft宣传软文是什么意思
  • 网站的日常维护友情链接例子
  • 做公司的网站付的钱怎么入账脱发严重是什么原因引起的
  • 商城开源免费商用谷歌seo优化排名
  • 南京做企业网站长尾关键词挖掘站长工具
  • 珠海做网站优化的公司seo挖关键词
  • 急切网头像在线制作图片seo优化点击软件
  • 怎么制作一个动态网站青岛百度竞价
  • 成都网站建设市场分析网站收录什么意思
  • 网站推广的意义和方法今天上海最新新闻事件
  • 二级学院网站建设自评报告百度经验悬赏任务平台
  • 定制网站开发者有权利倒卖吗磁力bt种子搜索
  • 广州网站建设优化aso关键词覆盖优化
  • org.cn的网站备案条件百度全网营销
  • 网站推广服务方案百度推广怎么弄
  • c2c网站怎么做做谷歌推广比较好的公司
  • 哪个网站做兼职北京网站快速优化排名
  • 大连网络推广网站优化找哪家好google优化推广
  • 金坛网站建设山西网络推广
  • 威宁做网站百度收录教程
  • web网站设计的要求互联网广告精准营销
  • 商城购物网站建设方案怎么自己做个网站
  • 移动商务网站开发课程青岛网站建设
  • 设计商城商务网站视频推广
  • 全功能多国语言企业网站十大免费无代码开发软件
  • 手工建站与模板网站的区别营销网站系统
  • 北京市房山建设培训学校网站郑州百度seo网站优化
  • 生成图片的软件广州网站优化服务商
  • 网站开发虚拟主机是什么营销策略的重要性
  • 做DJ网站违法吗免费的外贸网站推广方法