Spring配置数据库连接

2023-05-30,,

<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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" xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="false"/>
<property name="locations">
<list>
<value>classpath*:/dbConfig.properties</value>
<value>classpath*:/security-config.properties</value>
<value>classpath*:/led_mq_config.properties</value>
</list>
</property>
</bean> <!-- 注解扫描的包 -->
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
<context:component-scan base-package="cn.com.digicube">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- uses @Transactional sign -->
<mvc:annotation-driven/>
<!-- uses @AspectJ sign-->
<aop:aspectj-autoproxy/>
<task:annotation-driven /> <!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="driverClassName" value="${operation.hibernate.driverClassName}"/>
<property name="url" value="${operation.hibernate.connection.url}"/>
<property name="username" value="${operation.hibernate.connection.username}"/>
<property name="password" value="${operation.hibernate.connection.password}"/>
<property name="maxActive" value="256"/>
<property name="initialSize" value="16"/>
<property name="maxWait" value="60000"/>
<property name="minIdle" value="16"/>
<property name="dbType" value="sqlserver"/>
<!--<propenamename="dbType" value = "sqlserver" />--> <property name="timeBetweenEvictionRunsMillis" value="3000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/> <property name="removeAbandoned" value="true"/>
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandonedTimeout" value="1800"/>
<!-- 1800秒,也就是30分钟 -->
<property name="logAbandoned" value="true"/>
<!-- 关闭abanded连接时输出错误日志 --> <property name="validationQuery" value="SELECT 1"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- mysql 不支持 poolPreparedStatements <propenamename="poolPreparedStatements"
value="true" /> <propenamename="maxPoolPreparedStatementPerConnectionSize"
value="20" /> -->
<!-- 配置过滤器:wall——防注入攻击(WallFilter默认的防注入配置,也可以自己另外配置),stat-监控统计功能 -->
<property name="filters" value="wall,stat"/>
</bean> <bean id="customNamingStrategy" class="cn.telemedias.commons.api.commons.extension.CustomNamingStrategy"></bean>
<!-- 配置hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="namingStrategy" ref="customNamingStrategy"/>
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${operation.hibernate.dialect}</prop>
<!-- <prop key="hibernate.default_schema">${hibernate.default_schema}</prop>-->
<prop key="hibernate.show_sql">${hibernate.connection.show_sql}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.connection.format_sql}</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<!--
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</prop>
-->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.SingletonEhCacheRegionFactory
</prop>
<!-- 二级缓存EhCache配置 -->
<!--<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>-->
<!--<prop key="hibernate.cache.provider_class">${hibernate.cache.region.factory_class}</prop>-->
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop>
<!-- 项目启动时初始化CurrentSessionContext,用于获取session(sessionFactory.getCurrentSession()) -->
<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>cn.com.itmy</value>
</list>
</property>
</bean> <!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 开启aop -->
<aop:config proxy-target-class="true"><!-- expose-proxy="true"-->
<aop:pointcut id="txPointcut" expression="execution(* cn.*.*.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config> <bean class="cn.telemedias.commons.api.commons.context.SystemContext" lazy-init="false"/>
<import resource="classpath:applicationContext-cache.xml"/>
<import resource="classpath:applicationContext-shiro.xml"/>
<import resource="classpath:applicationContext-mq.xml"/>
</beans>

Spring配置数据库连接的相关教程结束。

《Spring配置数据库连接.doc》

下载本文的Word格式文档,以方便收藏与打印。