【问题标题】:Spring : Cannot convert value of type TaskExecutorImpl to java.util.concurrent.ExecutorSpring:无法将 TaskExecutorImpl 类型的值转换为 java.util.concurrent.Executor
【发布时间】:2017-11-02 05:09:25
【问题描述】:

我有以下 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 错误 localhost-startStop-1 ContextLoader:331 - 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.context.annotation.internalAsyncAnnotationProcessor”的bean时出错:bean初始化失败;嵌套异常是 org.springframework.beans.ConversionNotSupportedException:无法将类型“com.xxx.xxx.management.common.executor.TaskExecutorImpl”的属性值转换为属性“executor”所需的类型“java.util.concurrent.Executor” ;嵌套异常是 java.lang.IllegalStateException:无法将类型 [com.xxx.xxx.management.common.executor.TaskExecutorImpl] 的值转换为属性“执行器”所需的类型 [java.util.concurrent.Executor]:没有匹配的编辑器或找到转化策略 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 在 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) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853) 在 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) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 在 java.lang.Thread.run(Thread.java:748) 原因:org.springframework.beans.ConversionNotSupportedException:无法将类型“com.messages.nsx.management.common.executor.TaskExecutorImpl”的属性值转换为属性“executor”所需的类型“java.util.concurrent.Executor” ;嵌套异常是 java.lang.IllegalStateException:无法将类型 [com.xxx.xxx.management.common.executor.TaskExecutorImpl] 的值转换为属性“执行器”所需的类型 [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] 的值转换为属性“执行器”所需的类型 [java.util.concurrent.Executor]:没有匹配的编辑器或找到转化策略 在 org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267) 在 org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:449) ... 29 更多

【问题讨论】:

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


    【解决方案1】:

    根据堆栈跟踪,TaskExecutorImpl 没有实现java.util.concurrent.Executor。而我们在 TaskExecutorImpl 类中真的看不到这一点。

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

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

    【讨论】:

    • TaskExecutor 是一个简单的接口。 Executor 是具体类的成员变量。
    • 好吧,如果这个 bean 与 Spring 上下文中的异步基础设施完全无关,请考虑将您的 taskExecutor 重命名为其他名称,不要混淆 AsyncAnnotationPostProcessor
    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2014-02-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    相关资源
    最近更新 更多