Spring:无法将TaskExecutorImpl类型的值转换为java.util.concurrent.Executor

时间:2017-06-01 07:18:08

标签: java spring spring-mvc spring-boot spring-integration

我有以下bean声明xml。

  <bean id="taskExecutor" class="com.XXX.xxx.management.common.executor.TaskExecutorImpl"
        destroy-method="shutdown">
        <constructor-arg name="threadPoolSize" value="5"/>
        <constructor-arg name="executionQueueCapacity" value="1000"/>
        <constructor-arg name="typeLimit" value="5"/>
    </bean>

和以下是TaskExecutorImpl的代码。

    import java.util.AbstractQueue;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.List;
    import java.util.TreeSet;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.CancellationException;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.FutureTask;
    import java.util.concurrent.RejectedExecutionException;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    import java.util.concurrent.atomic.AtomicInteger;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;

    import javax.annotation.PostConstruct;

    public class TaskExecutorImpl implements TaskExecutor {
        private final int threadPoolSize;
        private final int executionQueueCapacity;
        private final int typeLimit;

        private BlockingPriorityTaskQueue executionQueue;
        private ExecutorService threadPool;

        private ConcurrentHashMap<String, AtomicInteger> executionByTypeMap = new ConcurrentHashMap<>();

        /**
         * Main lock guarding all access
         */
        final ReentrantLock mainLock;
        /**
         * Condition for waiting takes
         */
        private final Condition notEmpty;
        /**
         * Condition for waiting puts
         */
        private final Condition notFull;

        public TaskExecutorImpl(int threadPoolSize, int executionQueueCapacity, int typeLimit) {
            this.threadPoolSize = threadPoolSize;
            this.executionQueueCapacity = executionQueueCapacity;
            this.typeLimit = typeLimit;

            mainLock = new ReentrantLock();
            notEmpty = mainLock.newCondition();
            notFull = mainLock.newCondition();
        }

        @PostConstruct
        @SuppressWarnings("unchecked")
        public void initialize() {
            executionQueue = new BlockingPriorityTaskQueue(executionQueueCapacity);
            threadPool = new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, TimeUnit.MILLISECONDS,
                    (BlockingQueue) executionQueue);

            // create core threads, required for type control
            for (int i = 0; i < threadPoolSize; i++) {
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        // nothing here
                    }
                });
            }
        } 

and other Methdods implementation so on

当我开始上下文时,我得到以下错误

  

2017-06-01 06:50:22.625 GMT ERROR localhost-startStop-1 ContextLoader:331 - 上下文初始化失败   org.springframework.beans.factory.BeanCreationException:使用名称&#39; org.springframework.context.annotation.internalAsyncAnnotationProcessor&#39;创建bean时出错:bean的初始化失败;嵌套异常是org.springframework.beans.ConversionNotSupportedException:无法转换类型&#39; com.xxx.xxx.management.common.executor.TaskExecutorImpl&#39;的属性值。要求的类型&#39; java.util.concurrent.Executor&#39;对于财产执行者&#39 ;;嵌套异常是java.lang.IllegalStateException:无法将[com.xxx.xxx.management.common.executor.TaskExecutorImpl]类型的值转换为属性&#39; executor&#39所需的类型[java.util.concurrent.Executor] ;:找不到匹配的编辑器或转换策略       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:293)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)       在org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:743)       在org.springvmframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)       在org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)       在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)       在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)       at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)       at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)       在org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:753)       在org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:729)       在org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)       在org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1092)       在org.apache.catalina.startup.HostConfig $ DeployDirectory.run(HostConfig.java:1834)       at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)       at java.util.concurrent.FutureTask.run(FutureTask.java:266)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:748)   引起:org.springframework.beans.ConversionNotSupportedException:无法转换类型&#39; com.messages.nsx.management.common.executor.TaskExecutorImpl&#39;的属性值。要求的类型&#39; java.util.concurrent.Executor&#39;对于财产执行者&#39 ;;嵌套异常是java.lang.IllegalStateException:无法将[com.xxx.xxx.management.common.executor.TaskExecutorImpl]类型的值转换为属性&#39; executor&#39所需的类型[java.util.concurrent.Executor] ;:找不到匹配的编辑器或转换策略       在org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:464)       在org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:495)       在org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:489)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1465)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1424)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1160)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)       ......还有23个   引起:java.lang.IllegalStateException:无法将[com.xxx.xxx.management.common.executor.TaskExecutorImpl]类型的值转换为属性&#39; executor&#39所需的类型[java.util.concurrent.Executor] ;:找不到匹配的编辑器或转换策略       在org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)       在org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:449)       ... 29更多

1 个答案:

答案 0 :(得分:0)

根据TaskExecutorImpl未实现java.util.concurrent.Executor的堆栈跟踪。我们真的在TaskExecutorImpl课程中看不到。

它实现了一些TaskExecutor,但我们无法说出哪一个,因为它没有导入。这只能说我们这个接口在同一个包中。

因此,请确保实现适当的接口以满足依赖注入要求。