之前用spring2+hibernate3+struts2開發了一個彩信發布系統,由於第一次使用此架構,造成applicationContext.xml中的配置非常冗長,而且經常因為更改一個小配置項(例:數據庫ip、用戶名、密碼等)將此文件作修改,這及不利於項目維護,萬一粗心造成其他地方變動,會對本來正常的項目造成bug
其實那個項目我最后做了分隔,將applicationContext.xml分隔成好幾段,但是我覺得其實對於數據庫方面的配置,完全可以通過加載hibernate.cfg.xml配置文件來配置項目的sessionFactory,所以這個新項目我決定使用此方式
這里介紹一下spring加載sessionFactory的這2種方式
1、通過配置dataSource來配置sessionFactory
applicationContext.xml
1 <!-- 數據庫配置 --> 2 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 3 destroy-method="close"> 4 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 5 <property name="url" value="jdbc:mysql://192.168.0.2:3306/tuke_mmsmsys"></property> 6 <property name="username" value="admin"></property> 7 <property name="password" value="richard"></property> 8 9 <!-- Connection Pooling Info --> 10 <property name="maxActive" value="20" /> 11 <property name="maxIdle" value="5" /> 12 <property name="maxWait" value="5000" /> 13 <property name="validationQuery" value="select count(0) from admin" /> 14 </bean> 15 <bean id="sessionFactory" 16 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 17 <property name="dataSource"> 18 <ref bean="dataSource" /> 19 </property> 20 <property name="hibernateProperties"> 21 <props> 22 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 23 <prop key="hibernate.show_sql">true</prop> 24 </props> 25 </property> 26 <property name="mappingDirectoryLocations"> 27 <list> 28 <value> 29 classpath:com/tukechina/mms/pojos 30 </value> 31 </list> 32 </property> 33 </bean>
2、通過加載hibernate.cfg.xml來配置sessionFactory
applicationContext.xml
1 <bean id="sessionFactory" 2 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 3 <property name="configLocation" value="classpath:hibernate.cfg.xml"> 4 </property> 5 </bean>
hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 4 <hibernate-configuration> 5 <session-factory name="mysql"> 6 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 7 <property name="hibernate.connection.password">1234</property> 8 <property name="hibernate.connection.url">jdbc:mysql://localhost/goodshool</property> 9 <property name="hibernate.connection.username">root</property> 10 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 11 <property name="hibernate.show_sql">true</property> 12 13 <!-- 最大連接數 --> 14 <property name="hibernate.c3p0.max_size">20</property> 15 <!-- 最小連接數 --> 16 <property name="hibernate.c3p0.min_size">5</property> 17 <!-- 獲得連接的超時時間,如果超過這個時間,會拋出異常,單位毫秒 --> 18 <property name="hibernate.c3p0.timeout">120</property> 19 <!-- 最大的PreparedStatement的數量 --> 20 <property name="hibernate.c3p0.max_statements">100</property> 21 <!-- 每隔120秒檢查連接池里的空閑連接 ,單位是秒--> 22 <property name="hibernate.c3p0.idle_test_period">120</property> 23 <!-- 當連接池里面的連接用完的時候,C3P0一下獲取的新的連接數 --> 24 <property name="hibernate.c3p0.acquire_increment">2</property> 25 26 <!-- 每次都驗證連接是否可用 --> 27 <property name="hibernate.c3p0.validate">true</property> 28 <mapping resource="com/shangx/pojos/User.hbm.xml" /> 29 </session-factory> 30 </hibernate-configuration>
對於第二種配置方案,找到的資料很少,大多數采用第一種,其實還有一種較好的配置
3、通過配置jdbc.properties文件分離數據庫的配置
jdbc.properties
1 Mysqljdbc.driverClassName=com.mysql.jdbc.Driver 2 Mysqljdbc.url=jdbc:mysql://localhost/goodshool 3 Mysqljdbc.username=root 4 Mysqljdbc.password=1234 5 6 # second cache statistics 7 hibernate.generate_statistics=true 8 9 # Property that determines the Hibernate dialect 10 # (only applied with "applicationContext-hibernate.xml") 11 hibernate.dialect=org.hibernate.dialect.MySQLDialect 12 hibernate.show_sql=true
applicationContext.xml
1 <bean id="propertyConfigurer" 2 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 3 <property name="location" value="classpath:jdbc.properties" /> 4 </bean> 5 6 <!-- 數據庫配置 --> 7 <bean id="mysqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 8 destroy-method="close"> 9 <property name="driverClass"> 10 <value>${Mysqljdbc.driverClassName}</value> 11 </property> 12 <property name="jdbcUrl"> 13 <value>${Mysqljdbc.url}</value> 14 </property> 15 <property name="user"> 16 <value>${Mysqljdbc.username}</value> 17 </property> 18 <property name="password"> 19 <value>${Mysqljdbc.password}</value> 20 </property> 21 22 <!-- 最小連接數 --> 23 <property name="minPoolSize"> 24 <value>5</value> 25 </property> 26 <!-- 達到最大連接數后可以增加的連接數 個 --> 27 <property name="acquireIncrement"> 28 <value>2</value> 29 </property> 30 <!-- 最大連接數 --> 31 <property name="maxPoolSize"> 32 <value>20</value> 33 </property> 34 <!-- 最大閑置時間 秒 --> 35 <property name="maxIdleTime"> 36 <value>600</value> 37 </property> 38 <!-- 最大的PreparedStatement的數量 --> 39 <property name="maxStatements" value="100"></property> 40 <!-- 閑置的連接測試周期 (秒) --> 41 <property name="idleConnectionTestPeriod"> 42 <value>120</value> 43 </property> 44 </bean> 45 46 <bean id="sessionFactory" 47 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 48 <!-- 49 <property name="configLocation" value="classpath:hibernate.cfg.xml" 50 /> <property name="configurationClass" 51 value="org.hibernate.cfg.AnnotationConfiguration" /> 52 --> 53 <property name="dataSource"> 54 <ref bean="mysqlDataSource" /> 55 </property> 56 <property name="hibernateProperties"> 57 <props> 58 <prop key="hibernate.generate_statistics"> 59 ${hibernate.generate_statistics} 60 </prop> 61 <prop key="hibernate.dialect"> 62 ${hibernate.dialect} 63 </prop> 64 <prop key="hibernate.show_sql"> 65 ${hibernate.show_sql} 66 </prop> 67 </props> 68 </property> 69 <property name="mappingDirectoryLocations"> 70 <list> 71 <value> 72 classpath:com/shangx/pojos 73 </value> 74 </list> 75 </property> 76 </bean>