【问题标题】:Spring @Transactional value param with SpEL (Spring expression language)带有 SpEL 的 Spring @Transactional 值参数(Spring 表达式语言)
【发布时间】:2013-10-07 10:11:11
【问题描述】:

在我的一个服务类中,我有一些方法被这样注释:

@Transactional(value="foodb")
public Bar getMeSomething(){
}

我最近了解了 @Value 借助 Spring EL 的强大功能来获取一些存储在属性文件中的值。 比如

@Value("${my.db.name}")

这就像一个魅力。

现在我正在尝试做同样的事情

@Transactional(value="${my.db.name}") 

没有成功...

我得到以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '${my.db.name}' is defined: No matching PlatformTransactionManager bean found for qualifier '${my.db.name}' - neither qualifier match nor bean name match!

Spring 是否支持我正在尝试做的事情?

如何在 @Transactional 注释中获取 my.db.name 值

谢谢

【问题讨论】:

    标签: spring transactions spring-transactions spring-el


    【解决方案1】:

    不,不支持。

    这是 org.springframework.transaction.annotation.SpringTransactionAnnotationParser 的摘录

    public TransactionAttribute parseTransactionAnnotation(Transactional ann) {
        RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
        rbta.setPropagationBehavior(ann.propagation().value());
        rbta.setIsolationLevel(ann.isolation().value());
        rbta.setTimeout(ann.timeout());
        rbta.setReadOnly(ann.readOnly());
        rbta.setQualifier(ann.value()); // <<--- this is where the magic would be
        // if it was there, but it isn't
    

    【讨论】:

    • 好的,谢谢。不管怎样……我在@Transaction 注释中使用的值是事务管理器的限定符名称……而不是数据库的名称(顺便说一句是相同的……):) 谢谢
    猜你喜欢
    • 2014-01-14
    • 2012-02-22
    • 1970-01-01
    • 2011-10-04
    • 2011-07-16
    • 1970-01-01
    • 2012-05-24
    • 2013-12-10
    相关资源
    最近更新 更多