买完服务器怎么做网站,python做的网站源码,富顺住房和城乡建设厅网站,国外过期域名查询网站Spring和Spring MVC使用父子容器的设计模式#xff0c;主要是为了实现更好的模块化和隔离#xff0c;提高系统的灵活性和可维护性。具体来说#xff0c;Spring应用通常包含两个层次的容器#xff1a;根容器#xff08;Root WebApplicationContext#xff09;和子容器主要是为了实现更好的模块化和隔离提高系统的灵活性和可维护性。具体来说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 xmlnshttp://xmlns.jcp.org/xml/ns/javaee xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsdversion3.1!-- 根容器 --context-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/spring/root-context.xml/param-value/context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener!-- 子容器 --servletservlet-namedispatcher/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/spring/appServlet/servlet-context.xml/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-namedispatcher/servlet-nameurl-pattern//url-pattern/servlet-mapping
/web-app
根容器配置文件root-context.xml
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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 iddataSource classorg.springframework.jdbc.datasource.DriverManagerDataSourceproperty namedriverClassName valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/mydb/property nameusername valueroot/property namepassword valuepassword//bean!-- 事务管理器 --bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerproperty namedataSource refdataSource//bean!-- 扫描业务服务层组件 --context:component-scan base-packagecom.example.service/
/beans
子容器配置文件servlet-context.xml
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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-packagecom.example.controller/!-- 配置视图解析器 --bean classorg.springframework.web.servlet.view.InternalResourceViewResolverproperty nameprefix value/WEB-INF/views//property namesuffix value.jsp//bean
/beans
.总结
根容器管理通用的、全局的Bean如数据源、事务管理器等。子容器管理特定于Servlet的Bean如Controller、视图解析器等。父子容器的关系子容器继承根容器的Bean定义但可以覆盖或添加新的Bean定义实现了模块化和隔离。