当前位置:Gxlcms > 数据库问题 > 整理spring + mysql + redis + 测试 的配置格式 和源码

整理spring + mysql + redis + 测试 的配置格式 和源码

时间:2021-07-01 10:21:17 帮助过:12人阅读

<?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 <!-- &lt;!&ndash; 首页&ndash;&gt;--> 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 <!-- &lt;!&ndash;自定义监听,根据sessionid获取session&ndash;&gt;--> 51 <!-- <listener>--> 52 <!-- <listener-class>com.songs.monitor.MySessionListener</listener-class>--> 53 <!-- </listener>--> 54 </web-app> web.xml 技术图片
 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     <!--&lt;!&ndash;  拦截器&ndash;&gt;-->
38     <!--    <mvc:interceptors>-->
39     <!--        <mvc:interceptor>-->
40     <!--&lt;!&ndash;            拦截所有&ndash;&gt;-->
41     <!--            <mvc:mapping path="/**"/>-->
42     <!--&lt;!&ndash;  设置不拦截请求&ndash;&gt;-->
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     <!--&lt;!&ndash;拦截逻辑类&ndash;&gt;-->
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>
dispatcher-servlet.xml 技术图片
 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>
springcontext-service.xml 技术图片
 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>
springcontext-dao.xml 技术图片
 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"/>
56     </bean>
57 
58 
59 </beans>
redisConfigure.xml 技术图片
 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     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
10         <!-- 驱动 -->
11         <property name="driverClassName" value="${jdbc.driverClassName}"/>
12         <!-- url -->
13         <property name="url" value="${jdbc.url}"/>
14         <!-- 用户名 -->
15         <property name="username" value="${jdbc.username}"/>
16         <!-- 密码 -->
17         <property name="password" value="${jdbc.password}"/>
18     </bean>
19 
20     <!-- 配置 Mybatis的工厂 -->
21     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
22         <!-- 数据源 -->
23         <property name="dataSource" ref="dataSource"/>
24         <!-- 配置Mybatis的核心 配置文件所在位置 -->
25         <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
26         <!-- 配置pojo别名 -->
27         <property name="typeAliasesPackage" value="cn.cen2guo.clinic.entity"/>
28         <!--当mapper中的接口文件与xml文件在同一个包下但是不在同一级时-->
29         <!--需要指定mapper 的xml文件路径,如果在同一级则可不写-->
30         <!-- 否则会报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)-->
31         <property name="mapperLocations" value="classpath:cn/cen2guo/clinic/mapper/mapperXML/*.xml"/>
32     </bean>
33 
34     <!--扫描mapper接口, 写在此包下即可被扫描到 -->
35     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
36         <property name="basePackage" value="cn.cen2guo.clinic.mapper"/>
37     </bean>
38 </beans>
mysqlConfigure.xml 技术图片
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        xsi:schemaLocation="http://www.springframework.org/schema/beans
5     http://www.springframework.org/schema/beans/spring-beans.xsd">
6     <!-- 位置信息服务接口-->
7     <bean id="locationService" class="cn.cen2guo.clinic.service.serviceImpl.LocationServiceImpl"/>
8 
9

                  

	 	
                    
                    
                    
                    
                    
                

人气教程排行