web.xml中load-on-startup的作用

web.xml中load-on-startup的作用

我们在web.xml中配置servlet的时候会有个属性<load-on-startup></load-on-startup>,这里主要记一下它的作用,源码在后续记得好好看一下。

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the Container is free to load the servlet whenever it chooses.   If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.
意思大概:

  1. load-on-startup 元素标记容器是否应该在web应用程序启动的时候就加载这个servlet,(实例化并调用其init()方法)。
  2. 它的值必须是一个整数,表示servlet被加载的先后顺序。
  3. 如果该元素的值为负数或者没有设置,则容器会当Servlet被请求时再加载。
  4. 如果值为正整数或者0时,表示容器在应用启动时就加载并初始化这个servlet,值越小,servlet的优先级越高,就越先被加载。值相同时,容器就会自己选择顺序来加载。

转载:https://www.cnblogs.com/lemon-now/p/5542301.html

### SSM框架中的配置文件及其关系 #### 1. **web.xml** `web.xml` 是 Java Web 应用程序的部署描述符,用于定义应用程序的整体结构和初始化参数。它是整个项目的入口点。 - 定义 `DispatcherServlet` 的映射路径,负责拦截所有 HTTP 请求并将它们转发到 Spring MVC 控制器。 - 配置监听器(Listener),例如 `ContextLoaderListener`,用于加载全局上下文 `applicationContext.xml` 或其他类似的 XML 文件。 - 设置过滤器(Filter),如字符编码过滤器,确保请求和响应的编码一致性。 ```xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.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> ``` 此部分通过 `ContextLoaderListener` 初始化全局上下文,并由 `DispatcherServlet` 加载特定于 Spring MVC 的配置[^1]。 --- #### 2. **spring-mybatis.xml** 该文件主要用于 MyBatis 和 Spring 的集成,配置数据源、事务管理器以及 SQL Session 工厂。 - 数据源 (`DataSource`) 使用第三方库(如 Druid、C3P0)实现数据库连接池。 - 配置事务管理器 (`TransactionManager`) 来管理事务。 - 创建 `SqlSessionFactory` 并将其与 MyBatis 映射文件关联。 ```xml <!-- 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- Mapper 扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper" /> </bean> ``` 此部分实现了 MyBatis 和 Spring 的无缝集成,使得 DAO 层可以通过注解或接口方式访问数据库[^2]。 --- #### 3. **spring-mvc.xml** 这是 Spring MVC 的核心配置文件,主要关注前端控制器的行为和视图解析。 - 组件扫描仅限于 Controller 类型的 Bean。 - 配置视图解析器以指定 JSP 页面的位置和扩展名。 - 启用对静态资源的支持以及其他高级功能。 ```xml <context:component-scan base-package="com.ssm.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:default-servlet-handler/> <mvc:annotation-driven/> ``` 这部分专注于处理用户的请求并返回相应的视图[^1]。 --- #### 4. **mybatis-config.xml** MyBatis 的核心配置文件,通常包含通用设置和插件声明。 - 配置缓存机制。 - 指定日志记录工具。 - 添加自定义类型处理器或其他扩展组件。 ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 缓存设置 --> <settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> </settings> <!-- 日志记录 --> <typeAliases> <package name="com.example.model"/> </typeAliases> <!-- 插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"/> </plugins> </configuration> ``` 此文件提供了 MyBatis 运行所需的底层支持,而具体的 SQL 映射则交由单独的 Mapper 文件完成[^3]。 --- #### 配置关系总结 1. **web.xml** 负责启动容器并加载全局上下文和 DispatcherServlet。 2. **spring-mybatis.xml** 提供了持久层所需的数据源、事务管理和会话工厂。 3. **spring-mvc.xml** 处理业务逻辑和视图渲染。 4. **mybatis-config.xml** 则作为 MyBatis 的基础配置文件,补充了额外的功能选项。 这些文件相互协作,共同构建了一个完整的 SSM 架构应用。 --- #### 常见问题及解决方法 1. 如果服务无法找到对应的 Bean,则需确认是否正确注册了相关组件扫描范围或手动定义了 Bean 实例[^4]。 2. 当遇到数据库操作异常时,请检查 `sqlSessionFactory` 是否成功加载了正确的 Mapper 文件位置。 3. 若页面显示错误提示未找到视图名称,请验证视图解析器前缀和后缀是否匹配实际目录结构。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值