简体   繁体   English

当 transactionManager 未命名为“transactionManager”时

[英]When transactionManager is not named "transactionManager"

I am trying Spring 3(.0.2.RELEASE) and JPA2 and Hibernate 3.5.1-Final... One thing upsets me is that spring seems only accept a transaction Manager named "transactionManager"我正在尝试 Spring 3(.0.2.RELEASE) 和 JPA2 以及 Hibernate 3.5.1-Final... 让我感到不安的是 spring 似乎只接受名为“transactionManager”的事务管理器

If I don't name it "transactionManager", Spring will throws NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined.如果我不将其命名为“transactionManager”,Spring 将抛出NoSuchBeanDefinitionException:未定义名为“transactionManager”的 bean

Here is my config:这是我的配置:

<context:component-scan base-package="destiny.data.mining"/>

<context:annotation-config/>

<bean id="miningEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="mining"/>
</bean>

<bean id="miningTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
  <property name="entityManagerFactory" ref="miningEntityManagerFactory"/>
</bean>

<tx:advice id="txAdviceMining" transaction-manager="miningTransactionManager">
  <tx:attributes>
    <tx:method name="get*"    read-only="true"/>
    <tx:method name="save*"   propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="delete*" propagation="REQUIRED"/>
    <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
  </tx:attributes>
</tx:advice>  

<aop:config>
  <aop:pointcut id="methods" expression="execution(* destiny.utils.AbstractDao+.*(..))"/>
  <aop:advisor advice-ref="txAdviceMining" pointcut-ref="methods"/>
</aop:config>

<tx:annotation-driven transaction-manager="miningTransactionManager"/>  

In this config, an Entity Manager Factory is not necessarily named "entityManagerFactory", and "txAdvice" is not necessarily named "txAdvice", either.在此配置中,实体管理器工厂不一定命名为“entityManagerFactory”,“txAdvice”也不一定命名为“txAdvice”。 But I don't know why on earth Spring requires a transaction manager named "transactionManager"?但我不知道为什么Spring需要一个名为“transactionManager”的事务管理器?

Is there any way not to name a transaction manager "transactionManager"?有没有办法不将事务管理器命名为“transactionManager”? (I'm running multiple spring config files, so I try my best to avoid name-conflicting) (我正在运行多个 spring 配置文件,所以我尽力避免名称冲突)

test code:测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:mining.xml"})
public class MiningPersonDaoTest
{
  @Inject
  private EntityManagerFactory miningEntityManagerFactory;

  @Inject
  private MiningPersonDao miningPersonDao;


  @Transactional
  @Test
  public void testUpdate()
  {
    MiningPerson p = miningPersonDao.get(42L);
    p.setLocationName("OOXX");
    miningPersonDao.update(p);
    System.out.println(p);
  }
}

My understanding is that in the context of unit tests ( TransactionalTestExecutionListener ), the code that otherwise looks up the transaction manager is not used ( TransactionInterceptor#determineTransactionManager ). 我的理解是,在单元测试( TransactionalTestExecutionListener )的上下文中, 使用以其他方式查找事务管理器的代码( TransactionInterceptor#determineTransactionManager )。

You could try to annotate your test class with @TransactionConfiguration , which accepts a transactionManager attribute. 您可以尝试使用@TransactionConfiguration注释您的测试类,它接受transactionManager属性。 Not the most elegant way, but possibly the best option for the time being. 不是最优雅的方式,但可能是目前最好的选择。

Today, I ran into same issue when I was trying very trivial example of Spring-Data JPA. 今天,当我尝试Spring-Data JPA的非常简单的例子时,我遇到了同样的问题。 I had given custom name to transaction manager, "dmTransactionManager". 我给事务管理器“dmTransactionManager”提供了自定义名称。 To fix it I had to specify it, I had to specify it like below: 要解决它我必须指定它,我必须指定如下:

<jpa:repositories base-package="com.my.repository" transaction-manager-ref="dmTransactionManager"/>

From the following doc: https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#transaction-declarative-annotations来自以下文档: https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#transaction-declarative-annotations

You can omit the transaction-manager attribute in the <tx:annotation-driven/> tag if the bean name of the TransactionManager that you want to wire in has the name transactionManager.如果要连接的 TransactionManager 的 bean 名称名为 transactionManager,则可以省略 <tx:annotation-driven/> 标记中的 transaction-manager 属性。 If the TransactionManager bean that you want to dependency-inject has any other name, you have to use the transaction-manager attribute如果要依赖注入的 TransactionManager bean 有任何其他名称,则必须使用 transaction-manager 属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM