Spring(非Spring Boot环境)中如何及在哪里声明事务管理器
让@Transactional注解生效的事务管理器配置方法
@Transactional是Spring提供的事务管理注解,事务管理器不需要配置在hibernate.cfg.xml中,而是要在Spring的配置体系里(XML或Java配置类)与EntityManagerFactory一同声明,具体操作如下:
一、XML配置方式
- 配置EntityManagerFactory(基于Hibernate的JPA实现)
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 替换为你的实体类所在包路径 --> <property name="packagesToScan" value="com.yourproject.entity"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 根据你的数据库替换对应的方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop> </props> </property> </bean>
- 声明JpaTransactionManager,关联EntityManagerFactory
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean>
- 开启事务注解支持
<tx:annotation-driven transaction-manager="transactionManager"/>
注意:XML配置文件需要引入tx命名空间及对应的schema,示例头部:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
二、Java配置类方式
使用@Configuration注解的配置类,一站式完成数据源、EntityManagerFactory、事务管理器的配置,并开启事务注解支持:
@Configuration @EnableTransactionManagement // 开启事务注解支持 @EnableJpaRepositories(basePackages = "com.yourproject.repository") // 若使用Spring Data JPA需添加 public class PersistenceConfig { // 配置数据源(以HikariCP为例) @Bean public DataSource dataSource() { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/your_db"); config.setUsername("db_user"); config.setPassword("db_password"); return new HikariDataSource(config); } // 配置EntityManagerFactory @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); em.setPackagesToScan("com.yourproject.entity"); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(hibernateProperties()); return em; } // 声明事务管理器 @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; } // Hibernate专属配置 private Properties hibernateProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "update"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect"); return properties; } }
关键注意事项
- @Transactional必须加在Spring容器管理的Bean上(比如标注@Service、@Component的类或其方法),否则不会生效。
- 事务管理器类型要匹配持久化技术:使用JPA+Hibernate时用
JpaTransactionManager,纯Hibernate可以用HibernateTransactionManager,但推荐优先使用JPA规范的实现。 - hibernate.cfg.xml仅负责Hibernate自身的核心配置(如映射文件、二级缓存、方言等),不涉及Spring事务管理器的配置。
内容的提问来源于stack exchange,提问作者Gaetan L.
相关产品推荐
相关产品推荐

