spring配置
所作项目的spring常用配置文件,结合hibernate
1.config.properties文件//配置连接数据库的参数,和Hibernate的一些参数
Java代码 复制代码
#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
9. #jdbc.driver=org.hsqldb.jdbcDriver
10. #jdbc.url=jdbc:hsqldb:file:/${***.root}/WEB-INF/data/***
11. #jdbc.username=sa
12. #jdbc.password=
13. #hibernate.dialect=org.hibernate.dialect.HSQLDialect
14.
15. #for oracle
16. jdbc.driver=oracle.jdbc.driver.OracleDriver
17. jdbc.url=jdbc:oracle:thin:@192.168.8.***:1521:orcl
18. jdbc.username=bmsys2
19. jdbc.password=oracle9i
20. hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
21.
22.
23. #for sql server
24. #jdbc.driver=net.sourceforge.jtds.jdbc.Driver
25. #jdbc.url=jdbc:jtds:sqlserver://localhost:1433/***;SelectMethod=cursor;
26. #jdbc.username=sa
27. #jdbc.password=zhang
28. #hibernate.dialect=org.hibernate.dialect.SQLServerDialect
29.
30. jdbc.maxActive=30
31. jdbc.maxIdle=20
32. jdbc.maxWait=1000
33.
34.
35. hibernate.show_sql=true
36. hibernate.hbm2ddl.auto=
37. hibernate.format_sql=true
38. hibernate.cache.use_query_cache=false
39. hibernate.default_batch_fetch_size=16
#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
2.spring-config.xml; //读取config.properties,配置DataSource
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
5. <property name="location">
6. <value>/WEB-INF/config.properties</value>
7. </property>
8. </bean>
9. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
10. <property name="driverClassName">
11. <value>${jdbc.driver}</value>
12. </property>
13. <property name="url">
14. <value>${jdbc.url}</value>
15. </property>
16. <property name="username">
17. <value>${jdbc.username}</value>
18. </property>
19. <property name="password">
20. <value>${jdbc.password}</value>
21. </property>
22. <property name="maxActive">
23. <value>${jdbc.maxActive}</value>
24. </property>
25. <property name="maxIdle">
26. <value>${jdbc.maxIdle}</value>
27. </property>
28. <property name="maxWait">
29. <value>${jdbc.maxWait}</value>
30. </property>
31. </bean>
32. </beans>
<?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>
3.spring-hibernate.xml;//配置sessionFactory,transactionManager
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
5. <property name="dataSource">
6. <ref bean="dataSource" />
7. </property>
8. <property name="mappingResources">
9. <list>
10. <value>com/../*.hbm.xml</value>
11. </list>
12. </property>
13. <property name="hibernateProperties">
14. <props>
15. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
16. <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
17. <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
18. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
19. <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
20. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
21. <prop key="hibernate.connection.useUnicode">true</prop>
22. <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
23. <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
24. </props>
25. </property>
26. </bean>
27. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
28. <property name="sessionFactory">
29. <ref bean="sessionFactory" />
30. </property>
31. </bean>
32. </beans>
<?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>
4.spring-dao.xml;//配置所有Dao
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <!-- Finance-->
5. <bean id="xxxDao" class="com.xxx.XXXDAOHbm">
6. <property name="sessionFactory">
7. <ref bean="sessionFactory" />
8. </property>
9. </bean>
10. </beans>
<?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>
5.spring-service.xml;//配置service,声明事务,继承baseTransactionProxy
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
5. <property name="transactionManager" ref="transactionManager" />
6. <property name="transactionAttributes">
7. <props>
8. <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
9. <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
10. <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
11. <prop key="create*">PROPAGATION_REQUIRED</prop>
12. <prop key="update*">PROPAGATION_REQUIRED</prop>
13. <prop key="delete*">PROPAGATION_REQUIRED</prop>
14. <prop key="set*">PROPAGATION_REQUIRED</prop>
15. <prop key="add*">PROPAGATION_REQUIRED</prop>
16. <prop key="remove*">PROPAGATION_REQUIRED</prop>
17. <prop key="increase*">PROPAGATION_REQUIRED</prop>
18. <prop key="init*">PROPAGATION_REQUIRED</prop>
19. <prop key="back*">PROPAGATION_REQUIRED</prop>
20. <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
21. </props>
22. </property>
23. </bean>
24.
25. <!-- service implements -->
26. <bean id="XXXService" parent="baseTransactionProxy">
27. <property name="target">
28. <bean class="com.XXX.XXXServiceImpl">
29. <property name="sessionFactory">
30. <ref bean="sessionFactory" />
31. </property>
32. <property name="xxxDao">
33. <ref bean="xxxDao" />//所包含Dao
34. </property>
35. <property name="commonService">
36. <ref bean="otherService" />//所包含service
37. </property>
38. </bean>
39. </property>
<property name="proxyTargetClass">
<value>true</value>
</property>
40. </bean>
41. </beans>
注意上面要加上
<property name="proxyTargetClass">
<value>true</value>
</property>
这段,都则会出错。
1.config.properties文件//配置连接数据库的参数,和Hibernate的一些参数
Java代码 复制代码
#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
9. #jdbc.driver=org.hsqldb.jdbcDriver
10. #jdbc.url=jdbc:hsqldb:file:/${***.root}/WEB-INF/data/***
11. #jdbc.username=sa
12. #jdbc.password=
13. #hibernate.dialect=org.hibernate.dialect.HSQLDialect
14.
15. #for oracle
16. jdbc.driver=oracle.jdbc.driver.OracleDriver
17. jdbc.url=jdbc:oracle:thin:@192.168.8.***:1521:orcl
18. jdbc.username=bmsys2
19. jdbc.password=oracle9i
20. hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
21.
22.
23. #for sql server
24. #jdbc.driver=net.sourceforge.jtds.jdbc.Driver
25. #jdbc.url=jdbc:jtds:sqlserver://localhost:1433/***;SelectMethod=cursor;
26. #jdbc.username=sa
27. #jdbc.password=zhang
28. #hibernate.dialect=org.hibernate.dialect.SQLServerDialect
29.
30. jdbc.maxActive=30
31. jdbc.maxIdle=20
32. jdbc.maxWait=1000
33.
34.
35. hibernate.show_sql=true
36. hibernate.hbm2ddl.auto=
37. hibernate.format_sql=true
38. hibernate.cache.use_query_cache=false
39. hibernate.default_batch_fetch_size=16
#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
2.spring-config.xml; //读取config.properties,配置DataSource
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
5. <property name="location">
6. <value>/WEB-INF/config.properties</value>
7. </property>
8. </bean>
9. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
10. <property name="driverClassName">
11. <value>${jdbc.driver}</value>
12. </property>
13. <property name="url">
14. <value>${jdbc.url}</value>
15. </property>
16. <property name="username">
17. <value>${jdbc.username}</value>
18. </property>
19. <property name="password">
20. <value>${jdbc.password}</value>
21. </property>
22. <property name="maxActive">
23. <value>${jdbc.maxActive}</value>
24. </property>
25. <property name="maxIdle">
26. <value>${jdbc.maxIdle}</value>
27. </property>
28. <property name="maxWait">
29. <value>${jdbc.maxWait}</value>
30. </property>
31. </bean>
32. </beans>
<?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>
3.spring-hibernate.xml;//配置sessionFactory,transactionManager
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
5. <property name="dataSource">
6. <ref bean="dataSource" />
7. </property>
8. <property name="mappingResources">
9. <list>
10. <value>com/../*.hbm.xml</value>
11. </list>
12. </property>
13. <property name="hibernateProperties">
14. <props>
15. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
16. <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
17. <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
18. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
19. <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
20. <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
21. <prop key="hibernate.connection.useUnicode">true</prop>
22. <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
23. <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
24. </props>
25. </property>
26. </bean>
27. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
28. <property name="sessionFactory">
29. <ref bean="sessionFactory" />
30. </property>
31. </bean>
32. </beans>
<?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>
4.spring-dao.xml;//配置所有Dao
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <!-- Finance-->
5. <bean id="xxxDao" class="com.xxx.XXXDAOHbm">
6. <property name="sessionFactory">
7. <ref bean="sessionFactory" />
8. </property>
9. </bean>
10. </beans>
<?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>
5.spring-service.xml;//配置service,声明事务,继承baseTransactionProxy
Java代码 复制代码
1. <?xml version="1.0" encoding="GBK" ?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4. <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
5. <property name="transactionManager" ref="transactionManager" />
6. <property name="transactionAttributes">
7. <props>
8. <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
9. <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
10. <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
11. <prop key="create*">PROPAGATION_REQUIRED</prop>
12. <prop key="update*">PROPAGATION_REQUIRED</prop>
13. <prop key="delete*">PROPAGATION_REQUIRED</prop>
14. <prop key="set*">PROPAGATION_REQUIRED</prop>
15. <prop key="add*">PROPAGATION_REQUIRED</prop>
16. <prop key="remove*">PROPAGATION_REQUIRED</prop>
17. <prop key="increase*">PROPAGATION_REQUIRED</prop>
18. <prop key="init*">PROPAGATION_REQUIRED</prop>
19. <prop key="back*">PROPAGATION_REQUIRED</prop>
20. <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
21. </props>
22. </property>
23. </bean>
24.
25. <!-- service implements -->
26. <bean id="XXXService" parent="baseTransactionProxy">
27. <property name="target">
28. <bean class="com.XXX.XXXServiceImpl">
29. <property name="sessionFactory">
30. <ref bean="sessionFactory" />
31. </property>
32. <property name="xxxDao">
33. <ref bean="xxxDao" />//所包含Dao
34. </property>
35. <property name="commonService">
36. <ref bean="otherService" />//所包含service
37. </property>
38. </bean>
39. </property>
<property name="proxyTargetClass">
<value>true</value>
</property>
40. </bean>
41. </beans>
注意上面要加上
<property name="proxyTargetClass">
<value>true</value>
</property>
这段,都则会出错。