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

东莞活动网站设计模板阿里巴巴个人网站怎么做

东莞活动网站设计模板,阿里巴巴个人网站怎么做,上海申远建筑设计有限公司,附近的教育培训机构有哪些Spring和Spring MVC使用父子容器的设计模式,主要是为了实现更好的模块化和隔离,提高系统的灵活性和可维护性。具体来说,Spring应用通常包含两个层次的容器:根容器(Root WebApplicationContext)和子容器&…

Spring和Spring MVC使用父子容器的设计模式,主要是为了实现更好的模块化和隔离,提高系统的灵活性和可维护性。具体来说,Spring应用通常包含两个层次的容器:根容器(Root WebApplicationContext)和子容器(Child WebApplicationContext)。下面是详细解释:

1. 根容器(Root WebApplicationContext)

  • 作用:根容器是整个Web应用的基础容器,通常用于管理应用的通用Bean,如数据源(DataSource)、事务管理器(Transaction Manager)、业务服务(Service Layer)等。
  • 配置:根容器的配置通常在 web.xml 文件中通过 ContextLoaderListener 加载,配置文件通常是 applicationContext.xml。
  • 生命周期:根容器在整个Web应用的生命周期内一直存在,直到应用停止。

2. 子容器(Child WebApplicationContext)

  • 作用:子容器是每个Servlet(如 DispatcherServlet)的专用容器,用于管理与特定Servlet相关的Bean,如Controller、视图解析器(ViewResolver)、拦截器(Interceptor)等。
  • 配置:子容器的配置通常在 DispatcherServlet 的配置文件中加载,配置文件通常是 servlet-context.xml 或者通过 @Configuration 类配置。
  • 生命周期:子容器的生命周期与对应的Servlet相同,当Servlet初始化时创建,当Servlet销毁时销毁。

3. 父子容器的关系

  • 继承关系:子容器继承了根容器的所有Bean定义,但可以覆盖或添加新的Bean定义。
  • 隔离性:子容器中的Bean不会影响根容器中的Bean,从而实现了模块化和隔离。
  • 资源共享:子容器可以访问根容器中的Bean,但根容器不能访问子容器中的Bean。

4. 具体好处

  • 模块化:根容器管理通用的、全局的Bean,子容器管理特定于Servlet的Bean,这种分层设计使得应用结构更加清晰,易于维护和扩展。
  • 隔离性:子容器中的Bean不会影响根容器中的Bean,避免了不同模块之间的相互干扰,提高了系统的稳定性。
  • 资源共享:通过继承关系,子容器可以共享根容器中的Bean,减少了重复配置,提高了资源利用率。
  • 灵活性:可以轻松地添加新的Servlet和对应的子容器,而不会影响现有的应用结构。

5. 配置示例
web.xml 配置

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><!-- 根容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/root-context.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 子容器 --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

根容器配置文件(root-context.xml)

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 数据源 --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mydb"/><property name="username" value="root"/><property name="password" value="password"/></bean><!-- 事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 扫描业务服务层组件 --><context:component-scan base-package="com.example.service"/>
</beans>

子容器配置文件(servlet-context.xml)

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 启用Spring MVC注解驱动 --><mvc:annotation-driven/><!-- 扫描Controller组件 --><context:component-scan base-package="com.example.controller"/><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean>
</beans>

.总结

  • 根容器:管理通用的、全局的Bean,如数据源、事务管理器等。
  • 子容器:管理特定于Servlet的Bean,如Controller、视图解析器等。
  • 父子容器的关系:子容器继承根容器的Bean定义,但可以覆盖或添加新的Bean定义,实现了模块化和隔离。
http://www.yayakq.cn/news/26255/

相关文章:

  • 顺德电子画册网站建设在中国备案的网站服务器
  • 公司网站建设怎么做账春雨直播视频观看完整版
  • 做企业网站进行推广要多少钱站长工具如何使用
  • 自己这么做网站网站建设公司教程
  • 网站设计维护员优化 保证排名
  • 网站访问跳出率seo优化与推广招聘
  • 网站优化的重要性代还软件开发
  • 鲅鱼圈做网站网工资页多少钱一个月百度升级最新版本下载安装
  • 新手搭建论坛己做网站佛山技术支持 禅城企业网站
  • 江山市建设局网站网络营销环境的分析主要是
  • 网站需要收集什么建站资源漳州网站建设求职简历
  • 工厂弄个网站做外贸如何处理天元建设集团有限公司欠款
  • 网站推广排名优化网站制作公司兴田德润简介
  • 做网站建设怎么跑客户小型购物网站模板
  • 网站域名怎么登陆网站显示数据
  • 电商网站建设基本流程图山西太原室内设计学校
  • asp论坛网站源码最新的购物网站 开
  • 手机网站界面设计WordPress智能友链审核
  • 百度移动端网站win10优化大师是官方的吗
  • asp手机网站管理系统天津网站优化方案
  • 用ps如何做网站首页wordpress 工单插件
  • 设计行业网站海珠区住房和水务建设局网站
  • 昆明网站制作工具网站 建设的必要性
  • 计算机网站设计wordpress 换首页
  • dedecms网站安装网站名字和域名
  • 网站模板带手机站园林景观设计公司名称
  • 河北品牌网站建设最近在线直播免费观看
  • 龙岩网站制作设计wordpress 单页海报
  • 做好我局门户网站建设工作如何制作h5页面视频
  • 河北正规网站建设比较公司注册网上核名通不过