可配置的交易超时:设置TX:建议/TX:属性/TX:Method/@timeout使用Spring EL

发布于 2025-02-11 19:29:56 字数 1562 浏览 1 评论 0原文

根据春季配置文件,我需要可配置的交易超时。因此,我想设置tx:建议/TX:属性/TX:Method/@timeout使用Spring EL。当我为默认值(BEANS)名称空间中的标签的属性进行类似设置时,它可以正常工作,而在TX名称空间中不进行。

看来模式验证在属性替代之前运行,或者根本没有替换:

Line 21 in XML document from class path resource [spring-core-main.xml] is invalid; 
nested exception is org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 148; 
cvc-datatype-valid.1.2.1: '#{transactionTimeoutSeconds}' is not a valid value for 'integer'.

我的配置看起来像:

<?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="transactionTimeoutSeconds" class="java.lang.Integer">
        <constructor-arg value="30" />
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" timeout="#{transactionTimeoutSeconds}" />
        </tx:attributes>
    </tx:advice>

有可能实现我想要使用XML配置的目标吗?

I need a configurable transaction timeout according to Spring profiles. So I would like to set tx:advice/tx:attributes/tx:method/@timeout using Spring EL. It works fine when I do a similar setup for an attribute of a tag in the default (beans) namespace, but not in the tx namespace.

It looks like that a schema validation runs before property substitution, or there is no substitution at this place at all:

Line 21 in XML document from class path resource [spring-core-main.xml] is invalid; 
nested exception is org.xml.sax.SAXParseException; lineNumber: 21; columnNumber: 148; 
cvc-datatype-valid.1.2.1: '#{transactionTimeoutSeconds}' is not a valid value for 'integer'.

My configuration looks like this:

<?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="transactionTimeoutSeconds" class="java.lang.Integer">
        <constructor-arg value="30" />
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" timeout="#{transactionTimeoutSeconds}" />
        </tx:attributes>
    </tx:advice>

Is it possible to achieve what I want using XML configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2025-02-18 19:29:56

看起来属性替代在此属性中不起作用。最终,我通过将整个TX内容放入个人资料来解决了问题:

<?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

  <beans profile="dev">
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" timeout="600" />
        </tx:attributes>
    </tx:advice>
  </beans>
  <beans profile="!dev">
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" timeout="30" />
        </tx:attributes>
    </tx:advice>
  </beans>

It looks like property substitution does not work in this attribute. Finally I solved the problem by putting the whole tx stuff into profiles:

<?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

  <beans profile="dev">
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" timeout="600" />
        </tx:attributes>
    </tx:advice>
  </beans>
  <beans profile="!dev">
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" timeout="30" />
        </tx:attributes>
    </tx:advice>
  </beans>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文