整理spring + mysql + redis + 测试 的配置格式 和源码
Posted 岑惜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了整理spring + mysql + redis + 测试 的配置格式 和源码相关的知识,希望对你有一定的参考价值。
经过多次整理,最终以这样的文件格式配置
目前配好的基本模板:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <!-- <!– 首页–>--> 7 <!-- <welcome-file-list>--> 8 <!-- <welcome-file>/WEB-INF/jsp/toIndex.jsp</welcome-file>--> 9 <!-- </welcome-file-list>--> 10 11 12 <!-- 加载spring容器 --> 13 <context-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value>classpath:spring/springcontext-*.xml</param-value> 16 </context-param> 17 <listener> 18 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 19 </listener> 20 21 <servlet> 22 <servlet-name>dispatcher</servlet-name> 23 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 24 <init-param> 25 <param-name>contextConfigLocation</param-name> 26 <param-value>classpath:spring/dispatcher-servlet.xml</param-value> 27 </init-param> 28 <load-on-startup>1</load-on-startup> 29 </servlet> 30 <servlet-mapping> 31 <servlet-name>dispatcher</servlet-name> 32 <url-pattern>/</url-pattern> 33 </servlet-mapping> 34 35 36 <!-- post请求乱码拦截器 --> 37 <filter> 38 <filter-name>CharacterEncodingFilter</filter-name> 39 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 40 <init-param> 41 <param-name>encoding</param-name> 42 <param-value>utf-8</param-value> 43 </init-param> 44 </filter> 45 <filter-mapping> 46 <filter-name>CharacterEncodingFilter</filter-name> 47 <url-pattern>/*</url-pattern> 48 </filter-mapping> 49 50 <!-- <!–自定义监听,根据sessionid获取session–>--> 51 <!-- <listener>--> 52 <!-- <listener-class>com.songs.monitor.MySessionListener</listener-class>--> 53 <!-- </listener>--> 54 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 <!-- 配置SpringMVC --> 13 <!-- 1.开启SpringMVC注解模式 --> 14 <!-- 简化配置: 15 (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter 16 (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持 17 --> 18 <mvc:annotation-driven/> 19 20 21 <!-- 2.静态资源默认servlet配置 22 (1)加入对静态资源的处理:js,gif,png 23 (2)允许使用"/"做整体映射 24 --> 25 <mvc:default-servlet-handler/> 26 27 <!-- 3.配置视图解析器--> 28 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 29 <property name="prefix" value="/WEB-INF/jsp/"/> 30 <property name="suffix" value=".jsp"/> 31 </bean> 32 33 <!-- 4.扫描web相关的bean --> 34 <context:component-scan base-package="cn.cen2guo.clinic.web"/> 35 36 37 <!--<!– 拦截器–>--> 38 <!-- <mvc:interceptors>--> 39 <!-- <mvc:interceptor>--> 40 <!--<!– 拦截所有–>--> 41 <!-- <mvc:mapping path="/**"/>--> 42 <!--<!– 设置不拦截请求–>--> 43 <!-- <mvc:exclude-mapping path="/**/fonts"/>--> 44 <!-- <mvc:exclude-mapping path="/**/*.css"/>--> 45 <!-- <mvc:exclude-mapping path="/**/*.js"/>--> 46 <!-- <mvc:exclude-mapping path="/**/*.png"/>--> 47 <!-- <mvc:exclude-mapping path="/**/*.gif"/>--> 48 <!-- <mvc:exclude-mapping path="/**/*.jpg"/>--> 49 <!-- <mvc:exclude-mapping path="/**/*.jpeg"/>--> 50 <!-- <mvc:exclude-mapping path="/**/*Login*"/>--> 51 <!-- <mvc:exclude-mapping path="/**/*login*"/>--> 52 <!-- <mvc:exclude-mapping path="/**/logOut"/>--> 53 <!--<!–拦截逻辑类–>--> 54 <!-- <bean class="com.songs.interceptor.MyInterceptor"/>--> 55 <!-- </mvc:interceptor>--> 56 <!-- </mvc:interceptors>--> 57 58 59 <!--文件上传 --> 60 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> 61 62 63 64 65 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd"> 15 <!-- 扫描service包下所有使用注解的类型 --> 16 <context:component-scan base-package="cn.cen2guo.clinic.service" /> 17 18 <!-- 配置事务管理器 --> 19 <bean id="transactionManager" 20 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 21 <!-- 注入数据库连接池 --> 22 <property name="dataSource" ref="dataSource" /> 23 </bean> 24 25 <!-- 通知 --> 26 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 27 <tx:attributes> 28 <!-- 传播行为 --> 29 <tx:method name="save*" propagation="REQUIRED"/> 30 <tx:method name="insert*" propagation="REQUIRED"/> 31 <tx:method name="delete*" propagation="REQUIRED"/> 32 <tx:method name="update*" propagation="REQUIRED"/> 33 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> 34 <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> 35 </tx:attributes> 36 </tx:advice> 37 <!-- 切面 --> 38 <aop:config> 39 <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.cen2guo.clinic.service.*.*(..))"/> 40 </aop:config> 41 42 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd"> 7 8 9 <!-- spring的属性加载器,加载所有properties文件中的属性,供所有springcontext-*.xml文件共同使用 --> 10 <bean id="configPropertyConfigurer" 11 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 12 <property name="locations"> 13 <list> 14 <!--这样写可以引入多个properties文件--> 15 <!-- <value>/WEB-INF/configInfo.properties</value> --> 16 <value>classpath:redis.properties</value> 17 <value>classpath:jdbc.properties</value> 18 </list> 19 </property> 20 </bean> 21 22 <!-- 导入redis配置文件--> 23 <import resource="classpath:redis/redisConfigure.xml"/> 24 25 <!--导入mysql配置文件--> 26 <import resource="classpath:mysql/mysqlConfigure.xml"/> 27 28 <!-- 导入自定义注册构造注入的bean--> 29 <import resource="classpath:myxml/my_javabean.xml"/> 30 31 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 7 8 9 <!-- Redis连接池配置 --> 10 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 11 <!-- 控制一个pool能分配多少个jedis实例 --> 12 <property name="maxTotal" value="${redis.pool.maxActive}"/> 13 <!-- 连接池中最多空闲多少个maxIdle个连接,这里为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,处于待命状态,随时连接 --> 14 <property name="maxIdle" value="${redis.pool.maxIdle}"/> 15 <!-- 最大等待时间,当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间即抛出异常 --> 16 <property name="maxWaitMillis" value="${redis.pool.maxWait}"/> 17 <!-- 在获取连接时,检查有效性 --> 18 <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> 19 </bean> 20 <!-- 创建Redis连接池,并做相关配置 --> 21 <bean id="jedisWritePool" class="cn.cen2guo.clinic.redis.JedisPoolWriper" 22 depends-on="jedisPoolConfig"> 23 <constructor-arg index="0" ref="jedisPoolConfig"/> 24 <constructor-arg index="1" value="${redis.hostname}"/> 25 <constructor-arg index="2" value="${redis.port}" type="int"/> 26 <constructor-arg index="3" value="${redis.timeout}" type="int"/> 27 <constructor-arg index="4" value="${redis.password}"/> 28 </bean> 29 <!-- 创建Redis工具类,封装好Redis的连接以进行相关操作 --> 30 <bean id="jedisUtil" class="cn.cen2guo.clinic.redis.JedisUtil" 31 > 32 <property name="jedisPool" ref="jedisWritePool"/> 33 </bean> 34 <!-- 这里的红色下划线提示不是指写错了,而是警告,如果使用自动补全修正,会报错UnsatisfiedDependencyException 35 为什么使用$进行隔开呢,是因为这是两个类嵌套定义,因为不是文件路径,无法使用.符号进行区分,故使用$符号时没问题的 36 --> 37 <bean id="jedisKeys" class="cn.cen2guo.clinic.redis.JedisUtil$Keys" 38 > 39 <constructor-arg ref="jedisUtil"/> 40 </bean> 41 <bean id="jedisStrings" class="cn.cen2guo.clinic.redis.JedisUtil$Strings" 42 > 43 <constructor-arg ref="jedisUtil"/> 44 </bean> 45 <bean id="jedisLists" class="cn.cen2guo.clinic.redis.JedisUtil$Lists" 46 > 47 <constructor-arg ref="jedisUtil"/> 48 </bean> 49 <bean id="jedisSets" class="cn.cen2guo.clinic.redis.JedisUtil$Sets" 50 > 51 <constructor-arg ref="jedisUtil"/> 52 </bean> 53 <bean id="jedisHash" class="cn.cen2guo.clinic.redis.JedisUtil$Hash" 54 > 55 <constructor-arg ref="jedisUtil"Java大厂技术文档:Redis+Nginx+设计模式+Spring全家桶+Dubbo精选