<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:component-scan base-package="com.hc.biz.impl" />
<!-- bean 默认单实例,配置scope="prototype"表示多实例,且访问时候才实例化对象 -->
<!-- spring对Hibernate的SessionFactory的整合 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 装配一个事务管理器《下面的事务模板需要》 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 定义事务通知,需要指定一个事务管理器 -->
<tx:advice id="tsadvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置哪些只读事务,哪些开启事务【配置所有方法不用事务,再配置所有需要开启事务的方法】 -->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 事务切入点 -->
<aop:pointcut id="serviceClass" expression="execution(* com.hc.biz.impl.*.*(..))" />
<!-- 将事务通知与应用规则的方法组合在一起 -->
<aop:advisor advice-ref="tsadvice" pointcut-ref="serviceClass" />
</aop:config>
</beans>
配置spring-context.xml 项目实例
最新推荐文章于 2022-03-10 12:12:25 发布