综合技术 --web.xml

 2023-09-05 阅读 32 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> 做了2个项目了,用的都是ssm整合框架,每次都是网上抄袭整合过程,整合过来可以用就行,不理解其中的原理。刚好这个项目做完了,闲了一段时间,在网上找了许多资料,大致的理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

做了2个项目了,用的都是ssm整合框架,每次都是网上抄袭整合过程,整合过来可以用就行,不理解其中的原理。刚好这个项目做完了,闲了一段时间,在网上找了许多资料,大致的理清楚了配置过程。

先放上我们最近一个项目的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><!-- 本项目采用如下介绍的第二种方式配置,同时指定加载文件的路径 --><servlet><servlet-name>caren</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- 拦截xx/caren/*后面所有的URL --><servlet-mapping><servlet-name>caren</servlet-name><url-pattern>/*</url-pattern></servlet-mapping><!-- 过滤编码格式为utf-8的内容,保证项目中使用中文时不出现乱码的情况 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><!-- 拦截。过滤 --><filter-mapping>  <filter-name>encodingFilter</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>  <!-- Log4j 配置 --><context-param><param-name>log4jRefreshInterval</param-name><param-value>10000</param-value></context-param><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener></web-app>

在网上查看了很多资料,都说web.xml加载顺序应该是:context-param-->listener-->filter-->servlet。

但是如上配置的web.xml中很明显没有context-param,也没有listener,log4j的配置除外(因为这个是可有可无的)。经过查资料后发现,网址:http://my.oschina.net/u/1172072/blog/383312

还是在这里写下网址中的内容

web项目中Spring的配置文件applicationcontext.xml是通过spring提供的加载机制,自动加载到容器中去的。spring提供了两种加载器,供web容器加载:

1>ContextLoaderListener

2>ContextLoaderServlet

这两种加载方式都可以满足需求,本项目采用的是第二种加载方式。

第一种方式配置如下

  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>

第二种方式配置如下 --本工程采用的方式

<servlet><servlet-name>context</servlet-name><servlet-class>org.springframework.context.ContextLoaderServlet</servlet-class><load-on-startup>1</load-on-startup>
</servlet>

通过上面的配置,web容器会自动加载applicationcontext.xml文件,如果需要指定配置文件的位置,可以通过context-param来指定,本项目就是指定的。个人理解,通过context-param来指定比默认的那种情况可靠,同时指定的时候,可以修改xxContext.xml的文件,这要比默认的灵活些。

<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>

好了,到此web.xml文件中的内容已经理解,按照web工程流程,接着应该去applicationContext.xml文件,我的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"xmlns:mvc="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd"><import resource="spring-mvc.xml" /><import resource="spring-mybatis.xml" /><bean id="propertiesReader"class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><value>classpath:config.properties</value>  </property></bean></beans>

解释:重点应该关注两个import中的文件,下面配置的那个bean是针对配置属性文件的,如果项目中没有配置属性文件,拿这个是可以不用管的。下面接着看下spring-mvc.xml文件,本项目配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 配置注解相关信息 --><mvc:annotation-driven><mvc:message-converters><beanclass="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="objectMapper"><bean class="com.zhiji.caren.utils.CustomerObjectMapper"></bean></property></bean></mvc:message-converters></mvc:annotation-driven><!-- 数据库配置文件,如果不用数据库配置文件,这里可以不用管 --><context:property-placeholder location="classpath:dbconfig.properties" /><!-- 如下三个文件中存在注解,需要扫描 --><context:component-scan base-package="com.zhiji.caren.controller" /><context:component-scan base-package="com.zhiji.caren.service" /><context:component-scan base-package="com.zhiji.caren.common" /><!-- 文件上传大小限制 --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 上传限制 5M --><property name="maxUploadSize" value="5120000" /></bean></beans>

解释:<mvc:annotaion-driven>这部分应该配置的是项目中的注解信息,这点我还没有弄得很明白,这里不多解释,直接贴出CustomerObjectMapper文件

public class CustomerObjectMapper extends ObjectMapper {private static final long serialVersionUID = 1L;{DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl();sp.setNullValueSerializer(new NullSerializer());this.setSerializerProvider(sp);}public class NullSerializer extends JsonSerializer<Object> {@Overridepublic void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)throws IOException, JsonProcessingException {jgen.writeString("");}}}

接着,我们应该到spring-mybatis.xml文件中,本项目的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
"><!-- JNDI方式配置数据源 --><!-- ========================配置数据源================================ --><!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 --><bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="url" value="${jdbc_url}" /><property name="username" value="${jdbc_username}" /><property name="password" value="${jdbc_password}" /><!-- 初始化连接大小 --><property name="initialSize" value="0" /><!-- 连接池最大使用连接数量 --><property name="maxActive" value="20" /><!-- 连接池最大空闲 --><property name="maxIdle" value="20" /><!-- 连接池最小空闲 --><property name="minIdle" value="0" /><!-- 获取连接最大等待时间 --><property name="maxWait" value="60000" /><!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --><!-- <property name="validationQuery" value="${validationQuery}" /> --><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><property name="testWhileIdle" value="true" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="25200000" /><!-- 打开removeAbandoned功能 --><property name="removeAbandoned" value="true" /><!-- 1800秒,也就是30分钟 --><property name="removeAbandonedTimeout" value="1800" /><!-- 关闭abanded连接时输出错误日志 --><property name="logAbandoned" value="true" /><!-- 监控数据库 --><!-- <property name="filters" value="stat" /> --><property name="filters" value="mergeStat" /></bean><!-- ===========================针对myBatis的配置项======================= --><!-- 配置sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 --><property name="dataSource" ref="dataSource" /><!-- 自动扫描me/gacl/mapping/目录下的所有SQL映射的xml文件, 省掉Configuration.xml里的手工配置value="classpath:me/gacl/mapping/*.xml"指的是classpath(类路径)下me.gacl.mapping包中的所有xml文件UserMapper.xml位于me.gacl.mapping包下,这样UserMapper.xml就可以被自动扫描--><property name="mapperLocations" value="classpath:com/zhiji/caren/mapping/*.xml" /></bean><!-- 配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 --><property name="basePackage" value="com.zhiji.caren.mapper" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean><!-- ==========================分隔线======================== --><!-- 配置Spring的事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- 注解方式配置事物 --><!-- <tx:annotation-driven transaction-manager="transactionManager" /> --><!-- 拦截器方式配置事物 --><tx:advice id="transactionAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" /><tx:method name="append*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="modify*" propagation="REQUIRED" /><tx:method name="edit*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="repair" propagation="REQUIRED" /><tx:method name="delAndRepair" propagation="REQUIRED" /><tx:method name="get*" propagation="SUPPORTS" /><tx:method name="find*" propagation="SUPPORTS" /><tx:method name="load*" propagation="SUPPORTS" /><tx:method name="search*" propagation="SUPPORTS" /><tx:method name="datagrid*" propagation="SUPPORTS" /><tx:method name="*" propagation="SUPPORTS" /></tx:attributes></tx:advice><aop:config><!-- 匹配com.zhiji.caren.service.impl这个类下面的所有方法--><aop:pointcut id="transactionPointcut" expression="execution(* com.zhiji.caren.service.impl.*Impl.*(..))" /><!--把事务控制在Service层--><aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /></aop:config><!-- 配置druid监控spring jdbc --><bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean><bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"><property name="patterns"><list><value>com.zhiji.service.impl.*</value></list></property></bean><aop:config><aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /></aop:config></beans>

数据库配置文件如下

driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://192.168.x.xx:3306/caren_db?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
#jdbc_url=jdbc:sqlserver://[serverName[\instanceName][:portNumber]]
jdbc_username=数据库名称
jdbc_password=数据库密码

spring-mybatis.xml这个文件中分为两大部分:数据库配置和事务配置。数据库那部分应该比较简单,事务配置那个部分,查看者自己再去查点资料对照理解下。

最后,我整理了几个写的不错的文章,可以参考下

http://my.oschina.net/fankun2013/blog/517535

http://www.cnblogs.com/hellojava/archive/2012/12/28/2835730.html

http://my.oschina.net/treenewbee/blog/489926


2015年11月26日补充

上述的事务配置是正确的。事务配置在service层,所以service层的方法命名必须要满足add*、insert*等。我测试过,可以满足回滚。只有当service层这个方法中的所有操作数据库的操作全部成功后,这个service层才返回成功。



转载于:https://my.oschina.net/u/2312022/blog/525435

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/1330.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息