2
votes

I'm trying to use the util:constant tag for ioc, but I'm getting the following error message:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name 'threadPoolExecutor' defined in class path resource [spring.xml]: Unknown property sub-element: <util:constant>

Here's my 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>

Update:

Here's my xml with the <value> tag added which causes a different error message:

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

(Sidenote: for some reason my formatting controls have disappeared when posting in 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

1 Answers

3
votes

For enum's you can directly assign the values and Spring will take care of binding it to the correct enum:

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

Also, your original entry worked for me perfectly:

<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>