【转】Spring配置文件的一般写法(集成hibernate)
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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 单一属性(配置properties文件.) --><bean id="propertyConfigurer" value="jdbc.properties" /><!-- 注意这里的路径问题,这里是放在src下. --></bean><!-- 多个属性 <bean id="propertyConfigurer" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /></bean><!-- sessionFactory --><bean id="sessionFactory" ref="dataSource" /> <property name="mappingResources"> <list> <value>cn/xg/hibernate/spring/User.hbm.xml</value><!--这里的映射路径问题,这种方法只能一个一个加--> <value>cn/xg/hibernate/spring/Group.hbm.xml</value> </list> <!-- 加载一个路径下的*.hbm.xml文件方法: <property name="mappingDirectoryLocations"> <list> <value>classpath:/cn/xg/spring/model</value> </list> </property> --> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_sql">true</prop> </props> </property></bean><!-- DAO实现类extends HibernateDaoSupport,注入sessionFactory --><bean id="userMgrImpl" ref="sessionFactory" /></bean><bean id="groupMgrImpl" ref="sessionFactory" /> <property name="userImpl" ref="userMgrImpl"/> <property name="transactionTemplate" ref="transactionTemplate"/></bean><!-- 事务管理 --><bean id="transactionManager" ref="sessionFactory" /></bean><!-- 编程式事务的写法 :向Dao实现类中注入transactionTemplate,调动其execute()方法,接口回调new TransactionCallback()--><bean id="transactionTemplate" ref="transactionManager"/></bean><!-- 声时式事务第一种写法 --><!-- <bean id="groupMgr" ref="transactionManager" /> <property name="target" ref="groupMgrImpl" /> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED</prop> <prop key="*">readOnly</prop> </props> </property> </bean>--> <!-- 声时式事务第二种写法 --><!-- 事务的传播特性<tx:advice id="txAdvice"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes></tx:advice><aop:config> <aop:advisor pointcut="execution(* cn.xg.hibernate.spring.*.*(..))" advice-ref="txAdvice" /></aop:config>--></beans>jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/数据库名jdbc.username=数据库用户名jdbc.password=数据库密码hibernate.dialect=org.hibernate.dialect.MySQLDialect(方言.这里是MySql)