<
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.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
<!--
要为 Hibernate提供SessionFactory,有两种方式获取SessionFactory:
1.根据hibernate.cfg.xml的配置文件来获取,由于在该配置文件中配置了连接数据库的相关信息,
所以这种方式不需要我们获取数据源,自动在内部完成。
2.手动根据properties文件获取数据源,然后再将数据源注入。
由于不能直接获取SessionFactory,因为SessionFactory不具备 di的特征(构造方法和set方法不满足),所以
Spring提供了一个额外的类:LocalSessionFactoryBean,它既有
hibernate
中sessionFactory中的功能
又有spring中DI的特点
-->
<!--方法一 直接根据hibernate的配置文件来获取 -->
<
bean
id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "configLocation">
<value> classpath:hibernate.cfg.xml</value >
</property>
</
bean
>
<!-- 方法二 自行获取数据源(先加载properties文件,然后获取数据源),然后将数据源注入 -->
<
bean
class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "locations">
<value> classpath:jdbc.properties</value >
</property>
</
bean
>
<
bean
id="dataSource" destroy-method="close"
class= "org.apache.commons.dbcp.BasicDataSource" >
<property name= "driverClassName" value="${jdbc.driverClassName}" />
<property name= "url" value ="${jdbc.url}" />
<property name= "username" value="${jdbc.username}" />
<property name= "password" value="${jdbc.password}" />
</
bean
>
<!-- 将获得的数据源注入到sessionFactory中-->
<
bean
id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "dataSource" ref ="dataSource"/>
<property name= "mappingResources">
<list>
<value> cn/itheima03/spring/ hibernate/transaction/domain/Classes.hbm.xml </value>
</list>
</property>
<property name= "hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</
bean
>
<!-- ===================================================== -->
<!-- 需要注入sessionFactory -->
<
bean
id="classesDao" class="cn.itheima03.spring.hibernate.transaction.dao.impl.ClassesDaoImpl" >
<property name= "sessionFactory">
<ref bean= "sessionFactory1"/>
</property>
</
bean
>
<
bean
id="classesService" class="cn.itheima03.spring.hibernate.transaction.service.impl.ClassesServiceImpl" >
<property name= "classesDao">
<ref bean= "classesDao"/>
</property>
</
bean
>
<!-- 引入hibernate实现的事务管理,注意使用的sessionFactory要和classDao中注入的是同一个! -->
<
bean
id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionFactory">
<ref bean= "sessionFactory1"/>
</property>
</
bean
>
<!-- 配置目标类的哪些方法使用什么样的事务 -->
<
tx:advice
id="tx" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name= "save*" read-only ="false"/>
</tx:attributes>
</
tx:advice
>
<
aop:config
>
<aop:pointcut expression="execution(* cn.itheima03.spring.hibernate.transaction.service.impl.ClassesServiceImpl.*(..))" id ="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref= "perform"/>
</
aop:config
>
</
beans
>
|