spring继承mybatis、mq、redis、httpclient常用配置

servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	
	<!-- 注解驱动 -->
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<constructor-arg index="0" value="UTF-8"></constructor-arg><!-- 向构造函数中第几个参数传值 -->
			</bean>
			<bean class="com.static.common.spring.extend.messageconvertor.json.CallbackMappingJackson2HttpMessageConverter">
				<property name="callbackName" value="callback"> </property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	
	<!-- 扫描Controller,尽可能配置到指定的路径 -->
	<context:component-scan base-package="com.static.manage.controller"/>
	
	<!--视图解析器  -->
	<!--
		prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" 
	  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
		<property name="order" value="2"></property>
	</bean>
	<!-- 过滤掉所有的静态资源,把静态资源交给服务器,spirngmvc自己不处理 -->
	<mvc:default-servlet-handler/>
	
	<!-- 要求id必须是 multipartResolver -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设定默认编码 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- 设定文件上传的最大值5MB,5*1024*1024,如果一次性上传多个文件,则只的时候所有文件的总和 -->
		<property name="maxUploadSize" value="5242880"></property>
	</bean>
</beans>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	
	<!-- 使用spring自带的占位符替换功能 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 允许JVM参数覆盖 -->
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<!-- 忽略没有找到的资源文件 -->
		<property name="ignoreResourceNotFound" value="true" />
		<!-- 配置资源文件 -->
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
				<value>classpath:env.properties</value>
				<value>classpath:redis.properties</value>
				<value>classpath:httpclient.properties</value>
				<value>classpath:rabbit.properties</value>
			</list>
		</property>
	</bean>
	
	<!-- 扫描包,扫描Service注解 -->
	<context:component-scan base-package="com.static"/>

	<!-- 定义数据源,使用自己实现的数据源 -->
	<bean id="dataSource" class="com.static.manage.datasource.DynamicDataSource">
		<!-- 设置多个数据源 -->
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<!-- 这个key需要和程序中的key一致 -->
				<entry key="master" value-ref="masterDataSource"/>
				<entry key="slave" value-ref="slave01DataSource"/>
			</map>
		</property>
		<!-- 设置默认的数据源,这里默认走写库 -->
		<property name="defaultTargetDataSource" ref="masterDataSource"/>
	</bean>


	<!-- 配置连接池 -->
	<bean id="masterDataSource" class="com.jolbox.bonecp.BoneCPDataSource"
		destroy-method="close">
		<!-- 数据库驱动 -->
		<property name="driverClass" value="${jdbc.master.driver}" />
		<!-- 相应驱动的jdbcUrl -->
		<property name="jdbcUrl" value="${jdbc.master.url}" />
		<!-- 数据库的用户名 -->
		<property name="username" value="${jdbc.master.username}" />
		<!-- 数据库的密码 -->
		<property name="password" value="${jdbc.master.password}" />
		<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
		<property name="idleConnectionTestPeriod" value="60" />
		<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
		<property name="idleMaxAge" value="30" />
		<!-- 每个分区最大的连接数 -->
		<property name="maxConnectionsPerPartition" value="150" />
		<!-- 每个分区最小的连接数 -->
		<property name="minConnectionsPerPartition" value="5" />
	</bean>
	
	<!-- 配置连接池 -->
	<bean id="slave01DataSource" class="com.jolbox.bonecp.BoneCPDataSource"
		destroy-method="close">
		<!-- 数据库驱动 -->
		<property name="driverClass" value="${jdbc.slave01.driver}" />
		<!-- 相应驱动的jdbcUrl -->
		<property name="jdbcUrl" value="${jdbc.slave01.url}" />
		<!-- 数据库的用户名 -->
		<property name="username" value="${jdbc.slave01.username}" />
		<!-- 数据库的密码 -->
		<property name="password" value="${jdbc.slave01.password}" />
		<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
		<property name="idleConnectionTestPeriod" value="60" />
		<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
		<property name="idleMaxAge" value="30" />
		<!-- 每个分区最大的连接数 -->
		<property name="maxConnectionsPerPartition" value="150" />
		<!-- 每个分区最小的连接数 -->
		<property name="minConnectionsPerPartition" value="5" />
	</bean>
	

</beans>

applicationContext-transaction.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 定义事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 定义事务策略 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!--定义查询方法都是只读的 -->
			<tx:method name="query*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="get*" read-only="true" />

			<!-- 主库执行操作,事务传播行为定义为默认行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />

			<!--其他方法使用默认事务策略 -->
			<tx:method name="*" />

		</tx:attributes>
	</tx:advice>

	<bean class="com.static.manage.datasource.DataSourceAspect" id="dataSourceAspect" />

	<aop:config>
		<!--pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型,
			这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配cn.itcast.mybatis.service包下的所有类的所有 
			方法 -->
		<aop:pointcut id="myPointcut" expression="execution(* com.static.manage.service.*.*(..))" />
		<!--将定义好的事务处理策略应用到上述的切入点 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
		
		<!-- 将切面应用到自定义的切面处理器上,-9999保证该切面优先级最高执行 -->
		<aop:aspect ref="dataSourceAspect" order="-9999">
			<aop:before method="before" pointcut-ref="myPointcut" />
		</aop:aspect>
		
	</aop:config>
	
</beans>

applicationContext-redis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 定义连接配置信息 -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxTotal}"></property>
	</bean>
	<!-- 配置redis的连接池 -->
	<bean class="redis.clients.jedis.ShardedJedisPool">
		<constructor-arg index="0" ref="poolConfig"></constructor-arg>
		<constructor-arg index="1">
			<list>
				<bean class="redis.clients.jedis.JedisShardInfo" >
					<constructor-arg index="0" value="${redis.node1.host}"></constructor-arg>
					<constructor-arg index="1" value="${redis.node1.port}"></constructor-arg>				
				</bean>
			</list>
		</constructor-arg>
	</bean>

</beans>

applicationContext-rabbitmq.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/rabbit
	http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
	<!-- 配置生产者 -->

	<!-- 定义连接工厂 -->
	<rabbit:connection-factory id="connectionFactory"
		host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}"
		password="${rabbit.password}" virtual-host="${rabbit.vhost}" />

	<!-- 配置rabbit管理器 -->
	<rabbit:admin connection-factory="connectionFactory" />

	<!-- 交换机 auto-declare="true" ,自动声明交换机 发布消息的时候,如果没有该交换机,才去生命 durable :是否持久化 -->
	<rabbit:topic-exchange name="STATIC-MANAGE-ITEM-EXCHANGE"
		auto-declare="true" durable="true"></rabbit:topic-exchange>

	<rabbit:template id="rabbitTemplate" exchange="STATIC-MANAGE-ITEM-EXCHANGE"
		connection-factory="connectionFactory"></rabbit:template>
</beans>

applicationContext-mybatis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	  <property name="dataSource" ref="dataSource" />
	  <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
	  <property name="typeAliasesPackage" value="com.static.manage.pojo"></property>
	  <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
	</bean>
	<!-- mapper接口扫描器  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 如果配置多个mapper的路径,可以使用,进行分割 -->
	  <property name="basePackage" value="com.static.manage.mapper" />
	</bean>
</beans>

applicationContext-httpclient.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	
	<!--定义连接管理器  -->
	<bean id="connectManage" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
		<property name="maxTotal" value="${httpclient.maxTotal}"></property>
		<property name="defaultMaxPerRoute" value="${httpclient.defaultMaxPerRoute}"></property>
	</bean>
	
	<!-- 定义httpclient的构造器 -->
	<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">
		<property name="connectionManager" ref="connectManage"></property>
	</bean>
	<!-- 配置成多例 -->
	<bean class="org.apache.http.impl.client.CloseableHttpClient" factory-bean="httpClientBuilder" factory-method="build"
	scope="prototype"></bean>
	
	<!-- 定义请求的构造器 -->
	<bean id="requstBuilder" class="org.apache.http.client.config.RequestConfig.Builder" >
		<property name="connectTimeout" value="${httpclient.connectTimeout}"></property>
		<property name="connectionRequestTimeout" value="${httpclient.connectionRequestTimeout}"></property>
		<property name="socketTimeout" value="${httpclient.socketTimeout}"></property>
		<property name="staleConnectionCheckEnabled" value="${httpclient.staleConnectionCheckEnabled}"></property>
	</bean>
	
	<!-- 定义请求的配置信息 -->
	<bean class="org.apache.http.client.config.RequestConfig" factory-bean="requstBuilder" factory-method="build"></bean>
	
	<!-- <bean class="com.static.common.httpclient.IdleConnectionEvictor" >
		<constructor-arg index="0" ref="connectManage"></constructor-arg>
	</bean> -->

</beans>







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值