spring 常用配置记录

1. mvc rest接口返回对象转字符串

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>

2.加载多个properties 文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
<!-- <property name="location" value="classpath:jdbc.properties" />-->
</bean>

3.数据库连接池(mybatis 配置):
<bean id="managerDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${manager.jdbc.url}"></property>
<property name="username" value="${manager.jdbc.username}"></property>
<property name="password" value="${manager.jdbc.password}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
</bean>


<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="managerSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="managerDataSource" />
<!-- <property name="configLocation" value="classpath:sqlMapConfig.xml"/> -->
<!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件-->
<property name="mapperLocations" value="classpath:mybatis/*Mapper.xml" />
<!--分页组件配置 -->
    <property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>


<!-- mapper接口namepspace绑定方式 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ytl.dao" /><!-- 多个使用逗号分隔 -->
<property name="sqlSessionFactoryBeanName" value="managerSqlSessionFactory" />
</bean>

4. redis 连接配置
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis_max_active}"></property>
<property name="maxIdle" value="${redis_max_idle}"></property>
<property name="maxWait" value="${redis_max_wait}"></property>
<property name="testOnBorrow" value="${redis_test_on_borrow}"></property>
</bean>
<!-- redis连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis_addr}"></property>
<property name="port" value="${redis_port}"></property>
<property name="password" value="${redis_auth}"></property>
<property name="poolConfig" ref="jedisConfig"></property>
</bean>

<!-- redis操作模板,这里采用尽量面向对象的模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>




posted @ 2021-03-25 14:11  笑对蓝天  阅读(32)  评论(0编辑  收藏  举报