Java Spring:获取错误“Unknown property sub-element:<util:constant>”</util:constant>

时间:2012-05-09 20:21:21

标签: java spring

我正在尝试为ioc使用util:constant标签,但是我收到以下错误消息:

  

线程“main”中的异常org.springframework.beans.factory.BeanDefinitionStoreException:在类路径资源[spring.xml]中定义名称为'threadPoolExecutor'的bean时出错:未知属性子元素:&lt; util:constant&gt;

这是我的xml:

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


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:config.properties</value>
    </property>
</bean>

<bean id="main" class="pikefin.Main">
<property name="executorSample" ref="executorSample"/>
</bean>


<bean id="executorSample" class="pikefin.ExecutorSample">
    <constructor-arg ref="threadPoolExecutor" />

</bean>


<bean id="threadPoolExecutor" class="java.util.concurrent.ThreadPoolExecutor">
    <constructor-arg index="0" value="2"/>
    <constructor-arg index="1" value="2"/>
    <constructor-arg index="2" value="10"/>
    <constructor-arg index="3"><util:constant static-field="java.util.concurrent.TimeUnit.SECONDS"/></constructor-arg>
    <constructor-arg index="4" ref="arrayBlockingPool"/>
</bean>

<bean id="arrayBlockingPool" class="java.util.concurrent.ArrayBlockingQueue">
    <constructor-arg value="5"/>
</bean>

</beans>

更新

这是添加了<value>标记的我的xml会导致不同的错误消息:

Invalid content was found starting with element 'util:constant'. No child element is expected at this point.

(旁注:出于某种原因,我在格式化控制时在SO中发布时消失了)

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


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:config.properties</value>
    </property>
</bean>

<bean id="main" class="pikefin.Main">
<property name="executorSample" ref="executorSample"/>
</bean>


<bean id="executorSample" class="pikefin.ExecutorSample">
    <constructor-arg ref="threadPoolExecutor" />

</bean>


<bean id="threadPoolExecutor" class="java.util.concurrent.ThreadPoolExecutor">
    <constructor-arg index="0" value="2"/>
    <constructor-arg index="1" value="2"/>
    <constructor-arg index="2" value="10"/>
    <constructor-arg index="3">
        <value>
            <util:constant static-field="java.util.concurrent.TimeUnit.SECONDS"/>
        </value>
    </constructor-arg>
    <constructor-arg index="4" ref="arrayBlockingPool"/>
</bean>

<bean id="arrayBlockingPool" class="java.util.concurrent.ArrayBlockingQueue">
    <constructor-arg value="5"/>
</bean>

</beans>

1 个答案:

答案 0 :(得分:3)

对于枚举,你可以直接赋值,Spring将负责将它绑定到正确的枚举:

<constructor-arg index="3" value="SECONDS">

此外,您的原始条目对我有用:

<bean id="threadPoolExecutor" class="java.util.concurrent.ThreadPoolExecutor">
    <constructor-arg index="0" value="2"/>
    <constructor-arg index="1" value="2"/>
    <constructor-arg index="2" value="10"/>
    <constructor-arg index="3"><util:constant static-field="java.util.concurrent.TimeUnit.SECONDS"/></constructor-arg>
    <constructor-arg index="4" ref="arrayBlockingPool"/>
</bean>