所作项目的spring常用配置文件,结合hibernate
1.config.properties文件//配置连接数据库的参数,和Hibernate的一些参数
[code]#for mysql
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/***?useUnicode=true&characterEncoding=GBK
#jdbc.username=root
#jdbc.password=123
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#for hsqldb
#jdbc.driver=org.hsqldb.jdbcDriver
#jdbc.url=jdbc:hsqldb:file:/${***.root}/WEB-INF/data/***
#jdbc.username=sa
#jdbc.password=
#hibernate.dialect=org.hibernate.dialect.HSQLDialect
#for oracle
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.8.***:1521:orcl
jdbc.username=bmsys2
jdbc.password=oracle9i
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
#for sql server
#jdbc.driver=net.sourceforge.jtds.jdbc.Driver
#jdbc.url=jdbc:jtds:sqlserver://localhost:1433/***;SelectMethod=cursor;
#jdbc.username=sa
#jdbc.password=zhang
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
jdbc.maxActive=30
jdbc.maxIdle=20
jdbc.maxWait=1000
hibernate.show_sql=true
hibernate.hbm2ddl.auto=
hibernate.format_sql=true
hibernate.cache.use_query_cache=false
hibernate.default_batch_fetch_size=16
[/code]
2.spring-config.xml; //读取config.properties,配置DataSource
[code]
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/config.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="maxActive">
<value>${jdbc.maxActive}</value>
</property>
<property name="maxIdle">
<value>${jdbc.maxIdle}</value>
</property>
<property name="maxWait">
<value>${jdbc.maxWait}</value>
</property>
</bean>
</beans>[/code]
3.spring-hibernate.xml;//配置sessionFactory,transactionManager
[code]<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/../*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
[/code]
4.spring-dao.xml;//配置所有Dao
[code]<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Finance-->
<bean id="xxxDao" class="com.xxx.XXXDAOHbm">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>[/code]
5.spring-service.xml;//配置service,声明事务,继承baseTransactionProxy
[code]<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="increase*">PROPAGATION_REQUIRED</prop>
<prop key="init*">PROPAGATION_REQUIRED</prop>
<prop key="back*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- service implements -->
<bean id="XXXService" parent="baseTransactionProxy">
<property name="target">
<bean class="com.XXX.XXXServiceImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="xxxDao">
<ref bean="xxxDao" />//所包含Dao
</property>
<property name="commonService">
<ref bean="otherService" />//所包含service
</property>
</bean>
</property>
</bean>
</beans>[/code]
1.config.properties文件//配置连接数据库的参数,和Hibernate的一些参数
[code]#for mysql
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/***?useUnicode=true&characterEncoding=GBK
#jdbc.username=root
#jdbc.password=123
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#for hsqldb
#jdbc.driver=org.hsqldb.jdbcDriver
#jdbc.url=jdbc:hsqldb:file:/${***.root}/WEB-INF/data/***
#jdbc.username=sa
#jdbc.password=
#hibernate.dialect=org.hibernate.dialect.HSQLDialect
#for oracle
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.8.***:1521:orcl
jdbc.username=bmsys2
jdbc.password=oracle9i
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
#for sql server
#jdbc.driver=net.sourceforge.jtds.jdbc.Driver
#jdbc.url=jdbc:jtds:sqlserver://localhost:1433/***;SelectMethod=cursor;
#jdbc.username=sa
#jdbc.password=zhang
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
jdbc.maxActive=30
jdbc.maxIdle=20
jdbc.maxWait=1000
hibernate.show_sql=true
hibernate.hbm2ddl.auto=
hibernate.format_sql=true
hibernate.cache.use_query_cache=false
hibernate.default_batch_fetch_size=16
[/code]
2.spring-config.xml; //读取config.properties,配置DataSource
[code]
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/config.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="maxActive">
<value>${jdbc.maxActive}</value>
</property>
<property name="maxIdle">
<value>${jdbc.maxIdle}</value>
</property>
<property name="maxWait">
<value>${jdbc.maxWait}</value>
</property>
</bean>
</beans>[/code]
3.spring-hibernate.xml;//配置sessionFactory,transactionManager
[code]<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/../*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
[/code]
4.spring-dao.xml;//配置所有Dao
[code]<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Finance-->
<bean id="xxxDao" class="com.xxx.XXXDAOHbm">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>[/code]
5.spring-service.xml;//配置service,声明事务,继承baseTransactionProxy
[code]<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="increase*">PROPAGATION_REQUIRED</prop>
<prop key="init*">PROPAGATION_REQUIRED</prop>
<prop key="back*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- service implements -->
<bean id="XXXService" parent="baseTransactionProxy">
<property name="target">
<bean class="com.XXX.XXXServiceImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="xxxDao">
<ref bean="xxxDao" />//所包含Dao
</property>
<property name="commonService">
<ref bean="otherService" />//所包含service
</property>
</bean>
</property>
</bean>
</beans>[/code]