【问题标题】:Transaction with Spring and Mybatis与 Spring 和 Mybatis 的交易
【发布时间】:2015-03-06 16:18:36
【问题描述】:

我有这个 mapper-config.xml。Game 类有 DAO 和 BLM:

<bean id="DAOGame" class="it.certimeter.nagima.dao.game.DAOGame">
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

<bean id="BLMGameTarget" class="it.certimeter.nagima.blm.game.BLMGame">
    <property name="daoGame" ref="DAOGame" />
</bean>

以及用于事务的 bean:

<bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" id="BLMGame">
    <property name="proxyInterfaces">
        <list>
            <value>it.certimeter.nagima.blm.game.IBLMGame</value>
        </list>
    </property>
    <property name="transactionManager">
        <ref bean="transactionManager"/>
    </property>
    <property name="target">
        <ref bean="BLMGameTarget"/>
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="saveGame">PROPAGATION_REQUIRED, -it.fondsai.jeffs.core.exception.service.appl.JeffsServiceApplException</prop>
        </props>
    </property>
    <!-- <property name="anonymousAccess" value="true"/> -->
</bean>

但是我有这个错误: 嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义类型 [it.certmeter.nagima.blm.game.IBLMGame] 的合格 bean:预期的单个匹配 bean 但找到了 2:BLMGameTarget,BLMGame

我哪里错了??

【问题讨论】:

标签: java spring spring-mvc transactions mybatis


【解决方案1】:

是的,Ben 评论的问题。前几天我自己也遇到了这个问题。

这个问题带来了不好的代码味道,但如果你想要一个解决方法,我建议如下:

转到您 @Autowire 您的 IBLMGame 实例的位置。除了@Autowire 注解之外,您还可以提供一个@Qualifier 注解并给出一个String 值,该值表示您实际想要连接的bean 名称。

所以对于你的情况,它可能看起来像这样:

@Autowired
@Qualifier("BLMGameTarget")  // you can substitute in "BLMGame" if that's the bean you want
IBLMGame iblmGame;

【讨论】:

  • 非常感谢 npm622。你为什么说它有一股难闻的代码味道?
  • “BLMGameTarget”和“BLMGame”听起来非常相关——就好像一个继承或实现另一个。我冒昧地猜测会有一种方法来重构您的代码,以便这些具体类中只有一个实现您正在自动装配的接口。或者,您可以将更具体的上下文类自动装配为字段类型。
猜你喜欢
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
相关资源
最近更新 更多