|
关于Spring的配置文件和数据源配置方式(MySQL数据库)
以下为自动注解Spring bean (Struts2 + Spring + hibernate)
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 数据库配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/bms2?characterEncoding=gbk"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1"/>
<!--连接池中保留的最小连接数。-->
<property name="minPoolSize" value="1"/>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>hbm.xml配置文件</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=none
hibernate.show_sql=false
hibernate.format_sql=false
</value>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 检查组件包 -->
<context:component-scan base-package="cn.com.sundy"/>
<!--使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
下面为Resources注解 (Struts2 + Spring + ibatis)
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 使用注解 -->
<context:annotation-config />
<!-- 配置SCDR数据源 -->
<bean id="scdrDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/scdr?zeroDateTimeBehavior=convertToNull" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 连接启动的初始值 -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减到maxIdle为止 -->
<property name="maxIdle" value="1" />
<!-- 最小空闲值,当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
<!-- 以固定间隔检查连接池中的空闲连接,把空闲时间超标的连接断开 -->
<property name="timeBetweenEvictionRunsMillis"><value>1800000</value></property>
<property name="minEvictableIdleTimeMillis"><value>3600000</value></property>
</bean>
<!-- 配置GCDR数据源 -->
<bean id="gcdrDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/gcdr?zeroDateTimeBehavior=convertToNull" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 连接启动的初始值 -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="500" />
<!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减到maxIdle为止 -->
<property name="maxIdle" value="1" />
<!-- 最小空闲值,当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
<!-- 以固定间隔检查连接池中的空闲连接,把空闲时间超标的连接断开 -->
<property name="timeBetweenEvictionRunsMillis"><value>1800000</value></property>
<property name="minEvictableIdleTimeMillis"><value>3600000</value></property>
</bean>
<!-- 配置事物管理器 -->
<bean id="scdrTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="scdrDataSource"/>
</bean>
<bean id="gcdrTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="gcdrDataSource"/>
</bean>
<!-- 采用注解方式 -->
<tx:annotation-driven transaction-manager="scdrTransactionManager" />
<tx:annotation-driven transaction-manager="gcdrTransactionManager" />
<!-- Spring提供的iBatis的SqlMap配置-->
<bean id="scdrSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="scdrDataSource"/>
</bean>
<bean id="gcdrSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="gcdrDataSource"/>
</bean>
<!--根据sqlMapClient创建一个SqlMapClient模版类-->
<bean id="scdrSqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="scdrSqlMapClient" />
</bean>
<bean id="gcdrSqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="gcdrSqlMapClient" />
</bean>
<!-- 配置dao -->
<bean id="userDao" class="com.ericsson.dao.impl.UserDaoImpl" />
<!-- 配置service -->
<bean id="loginAndOutService" class="com.ericsson.services.impl.LoginAndOutServiceImpl" />
</beans> |
|
|
|
|
|
|