为什么要同时使用bean id="transactionManager"和bean id="hibernateTemplate"呢?<?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:context="http://www.springframework.org/schema/context" 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/context
http://www.springframework.org/schema/context/spring-context-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"> <!-- 使用注解的方式,进行依赖注入 -->
<context:annotation-config /> <!-- 使用自动扫描机制,把@Component、@Service、@Controller、@Repository注解类,这些类纳入进spring容器中管理 -->
<context:component-scan base-package="cn.itcast" /> <!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 使用Hibernate的配置文件中的配置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- <property name="dataSource" ref="dataSource" /> -->
</bean> <!-- 事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 注解方式来打开事务, transaction-manager 为 transactionManager 时可以省略-->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" 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/context
http://www.springframework.org/schema/context/spring-context-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"> <!-- 使用注解的方式,进行依赖注入 -->
<context:annotation-config /> <!-- 使用自动扫描机制,把@Component、@Service、@Controller、@Repository注解类,这些类纳入进spring容器中管理 -->
<context:component-scan base-package="cn.itcast" /> <!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 使用Hibernate的配置文件中的配置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- <property name="dataSource" ref="dataSource" /> -->
</bean> <!-- 事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 注解方式来打开事务, transaction-manager 为 transactionManager 时可以省略-->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
是定义了一个hibernateTemplate实例
有什么区别呢?
hibernateTemplate用来简化session的操作
hibernateTemplate..整合框架时提供那些增删改查的模板。。
hibernateTemplate 是一个模版,如果你继承的是HibernateDaoSupport的话,就需要hibernateTemplate来获得数据库联接,否则没有连接到数据库,怎么操作数据库?
如果想自己在service层管理事物改怎么做呀?
你的业务类直接继承HibernateDaoTemplate就可以了,为什么要在xml中配置?
希望高手指点一下,thanks!