首页 > 解决方案 > Spring EL 无法在 xml 配置文件的 Integer 属性中解析

问题描述

我正在尝试在 XML 配置中使用 Spring EL:Creating a task executor with queue-capacity from an existing beans ConfigProp。

<task:executor id="executorWithPoolSizeRange" 
               pool-size="#{ConfigProp.async.corePoolSize}-#{ConfigProp.async.maxPoolSize}"
               queue-capacity="#{ConfigProp.async.queueCapacity}"/>

从 Intellij IDEA 中可以看出,pool-size可以正确解析,但不能为queue-capacity整数类型。

IDEA 投诉信息

Cannot convert string #{ConfigProp.async.queueCapacity} to target class java.lang.Integer"

使用 Tomcat 运行时的执行日志

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 129 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 129; columnNumber: 69; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'task:executor'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:402) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]

我做错了什么还是 SpringEL 不支持?

标签: javaspringspring-mvcspring-elxml-configuration

解决方案


我可以手动创建一个ThreadPoolTaskExecutorbean,但它仍然无法回答我的问题。

<beans:bean id="executorWithPoolSizeRange" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <beans:property name="corePoolSize" value="#{ConfigProp.async.corePoolSize}" />
        <beans:property name="maxPoolSize" value="#{ConfigProp.async.maxPoolSize}" />
        <beans:property name="queueCapacity" value="#{ConfigProp.async.queueCapacity}" />
    </beans:bean>

推荐阅读