1。屬性佔位符配置器:可以將相關屬性抽取出來放入配置文件(比如數據庫的連接信息),然後用佔位符的方式讀取出來,方便文件的分類管理
<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd語義約束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- PropertyPlaceholderConfigurer是一個Bean後處理器,它會讀取
屬性文件信息,並將這些信息設置成Spring配置文件的元數據。 -->
<bean class=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>dbconn.properties</value>
<!--如果有多個屬性文件,依次在下面列出來-->
<!--value>wawa.properties</value-->
</list>
</property>
</bean>
<!-- 定義數據源Bean,使用C3P0數據源實現 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定連接數據庫的驅動 -->
<property name="driverClass" value="${jdbc.driverClassName}"/>
<!-- 指定連接數據庫的URL -->
<property name="jdbcUrl" value="${jdbc.url}"/>
<!-- 指定連接數據庫的用戶名 -->
<property name="user" value="${jdbc.username}"/>
<!-- 指定連接數據庫的密碼 -->
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
2.重寫佔位符配置器:這種方式可以允許xml文件中有默認配置,相當於使用屬性文件+XML文件總和,但是需要注意的是配置文件的key的格式必須爲:beanName.property,即,bean的名字.屬性的名字
<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd語義約束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- PropertyOverrideConfigurer是一個Bean後處理器,它會讀取
屬性文件信息,並用這些信息設置覆蓋Spring配置文件的元數據-->
<bean class=
"org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<list>
<value>dbconn.properties</value>
<!--如果有多個屬性文件,依次在下面列出來-->
</list>
</property>
</bean>
<!-- 定義數據源Bean,使用C3P0數據源實現,
配置該Bean時沒有指定任何信息,但Properties文件裏的
信息將會直接覆蓋該Bean的屬性值-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"/>
</beans>
以上兩種配置如果導入了context的Schema,則可以使用<context:property-override location="屬性文件路徑(列如:classpath:XXX.properties)"/>或則<context:property-placeholder location="屬性文件路徑(列如:classpath:XXX.properties)"/>代替這兩個容器後處理器Bean的配置。
3.如果習慣了使用hibernate的配置風格,可以使用<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:configLocation="classpath:hibernate.cfg.xml"/>,配置多個文件則使用configLocations屬性即可。
4.使用Spring的配置風格,<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="數據源的Bean名"/>,
<!-- 依賴注入數據源,注入正是上面定義的dataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- mappingResouces屬性用來列出全部映射文件 -->
<property name="mappingResources">
<list>
<!-- 以下用來列出Hibernate映射文件 -->
<value>映射文件全路徑名</value>
</list>
</property>
<!-- 定義Hibernate的SessionFactory的屬性 -->
<property name="hibernateProperties">
<!-- 配置Hibernate屬性 -->
<value>
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true;
</value>
</property>