spring 框架搭建
程序员文章站
2022-04-22 07:58:48
...
<!--applicationContext.xml--> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <import resource="spring-hibernate.xml"/> <import resource="bean.xml"/> </beans>
<!--spring-hibernate.xml--> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/init.properties</value> </property> </bean> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>${datasource.driverClassName}</value> </property> <property name="url"> <value>${datasource.url}</value> </property> <property name="username"> <value>${datasource.username}</value> </property> <property name="password"> <value>${datasource.password}</value> </property> <property name="maxActive"> <value>${datasource.maxActive}</value> </property> <property name="maxIdle"> <value>${datasource.maxIdle}</value> </property> <property name="maxWait"> <value>${datasource.maxWait}</value> </property> <property name="defaultAutoCommit"> <value>${datasource.defaultAutoCommit}</value> </property> </bean> <!-- Hibernate DataSource --> <!-- <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"></property> <property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;databaseName=j2ee"> </property> <property name="username" value="sa"></property> <property name="password" value="sa"></property> </bean> --> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="myDataSource" /> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/citsoft/pojo</value> </list> </property> <!-- <property name="mappingResources"> <list> <value>com/citsoft/pojo/User.hbm.xml</value> </list> </property> --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop> <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> </props> </property> </bean> <!-- 定义事物管理器,使用适用于Hibernate的事物管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <!-- 配置事物拦截器 --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事物拦截器bean需要依赖注入一个事物管理器 --> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="transactionAttributes"> <!-- 定义事物的传播属性 --> <props> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 使用模板 --> <bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref local="sessionFactory"></ref> </property> </bean> </beans>
<!--bean.xml--> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!-- 定义BeanNameAutoProxyCreator,该bean是一个bean后处理器,无须被引用,因此没有id属性, 这个bean后处理器,根据事物拦截器为目标自动创建事物代理 --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <!-- <value>userDao</value>--> <value>tuserDao</value> <value>ttotalAccountDao</value> <value>taccountItemDao</value> <value>tprojectItemDao</value> </list> </property> <!-- 定义BeanNameAutoProxyCreator所需的事物拦截器 --> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> <value>myBeforeAdvice</value> <value>myAfterAdvice</value> <value>myAroundInterceptor</value> <value>runAdvisor</value> </list> </property> </bean> <!-- bean--> <bean id="BaseDaoImpl" class="com.citsoft.impl.BaseDAOImpl"> <property name="hibernateTemplate"> <ref bean="hibernateTemplete" /> </property> </bean> <!-- 登陆 begin --> <!-- <bean id="loginDao" class="com.qrsx.crm.dao.impl.LoginDAOImpl" parent="BaseDaoImpl"> </bean> --> <!-- end --> <!-- 问题库 begin --> <bean id="userDao" class="com.citsoft.impl.UserDAOImpl" parent="BaseDaoImpl"> </bean> <!-- end --> <!-- DaoBeans begin --> <bean id="daoBeans" class="com.citsoft.impl.DaoBeansImpl" > <property name="ttotalAccountDao"> <ref local="ttotalAccountDao"/> </property> <property name="taccountItemDao"> <ref local="taccountItemDao"/> </property> <property name="tprojectItemDao"> <ref local="tprojectItemDao"/> </property> <property name="tuserDao"> <ref local="tuserDao"/> </property> </bean> <!-- end --> <!-- TUserDao begin --> <bean id="commenDao" class="com.citsoft.impl.CommenDAOImpl" parent="BaseDaoImpl"> </bean> <!-- end --> <!-- 通知bean --> <bean id="myBeforeAdvice" class="com.citsoft.aop.MyBeforeAdvice"/> <bean id="myAfterAdvice" class="com.citsoft.aop.MyAfterAdvice"/> <bean id="myAroundInterceptor" class="com.citsoft.aop.MyAroundInterceptor"/> <bean id="runAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <bean class="com.citsoft.aop.MyAroundInterceptor"/> </property> <property name="patterns"> <list> <value>.*add.*</value> <value>.*update.*</value> <value>.*del.*</value> <value>.*update.*</value> <value>.*get.*</value> </list> </property> </bean> <!-- 通知bean --> <!-- TUserDao begin --> <bean id="tuserDao" class="com.citsoft.impl.TUserDaoImpl" parent="BaseDaoImpl"> </bean> <!-- end --> <!-- TTotalAccountDao begin --> <bean id="ttotalAccountDao" class="com.citsoft.impl.TTotalAccountDaoImpl" parent="BaseDaoImpl"> </bean> <!-- end --> <!-- TAccountItemDao begin --> <bean id="taccountItemDao" class="com.citsoft.impl.TAccountItemDaoImpl" parent="BaseDaoImpl"> </bean> <!-- end --> <!-- TProjectItemDao begin --> <bean id="tprojectItemDao" class="com.citsoft.impl.TProjectItemDaoImpl" parent="BaseDaoImpl"> </bean> <!-- end --> </beans>
#init.properties
#Created by JInto - www.guh-software.de
#Sat May 06 15:19:34 CST 2006 ?useUnicode=true&characterEncoding=GBK
#SQL SERVER 2000 config
#datasource.url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=j2ee
#datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
#datasource.username=sa
#datasource.password=sa
#MYSQL config
datasource.url=jdbc:mysql://127.0.0.1:3306/j2ee
datasource.driverClassName=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLDialect
datasource.username=root
datasource.password=root
datasource.defaultAutoCommit=true
datasource.maxActive=10
datasource.maxIdle=2
datasource.maxWait=120000
datasource.testOnBorrow=true
datasource.testOnReturn=false
datasource.validationQuery=select 1 from dual
datasource.whenExhaustedAction=1
hibernate.hbm2ddl.auto=create-drop
hibernate.jdbc.batch_size=25
hibernate.jdbc.fetch_size=50
hibernate.show_sql=true