扩展类会抛出UnsatisfiedDependencyException异常

时间:2016-08-21 03:37:35

标签: java spring spring-mvc

我是构建Web服务的新手,我开始使用spring boot来构建一个。我创建了以下控制器类

package controller;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import model.Time;
import service.FeedService;

@RestController
public class FeedController extends ScheduledThreadPoolExecutor{

    public FeedController(int corePoolSize) {
        super(corePoolSize);
        // TODO Auto-generated constructor stub
    }
    int difference;
    int a;
    boolean schedule;
    //static as the variable is accessed across multiple threads
    @Autowired
    static FeedService fs;

    @RequestMapping(value="/test")
    public String test(){
        return "test";
    }



    @RequestMapping(value = "/schedule", method = RequestMethod.GET)
    public int ScheduleFeed(@RequestParam(value = "difference",required = false) String y) throws InterruptedException, ExecutionException{
//      if(y != null){
//          difference = Integer.parseInt(y);
            difference = 0;
            NewScheduledThreadPoolTest.mymethod(difference);
            return difference;

//      }else{
//          return -1;
//      }
    }

    @RequestMapping(value = "/inquireSchedule", method = RequestMethod.GET)
    public ResponseEntity<Time> RequestFeed(){
        if(schedule == true){
            schedule = fs.setFalse();
            return new ResponseEntity<Time>(HttpStatus.OK);
        }
        return new ResponseEntity<Time>(HttpStatus.BAD_REQUEST);

    }
    //schedule the task to happen after a certain number of times
    static class NewScheduledThreadPoolTest {
        public static void mymethod(int difference, final String... args) throws InterruptedException, ExecutionException {
            // creates thread pool with 1 thread
            System.out.println("hello world");
            final ScheduledExecutorService schExService = Executors.newScheduledThreadPool(2);
            // Object creation of runnable thread.
            final Runnable ob = new NewScheduledThreadPoolTest().new myclass();
            // Thread scheduling ie run it after "difference" hours before and then after every 24 hours later on
            schExService.scheduleWithFixedDelay(ob, difference, 24, TimeUnit.SECONDS);
            // waits for termination for 30 seconds only
            schExService.awaitTermination(30, TimeUnit.SECONDS);
            // shutdown now.
            schExService.shutdownNow();
            System.out.println("Shutdown Complete");
        }

    class myclass implements Runnable{

        @Override
        public void run() {
            try{
            // the mechanism to give a positive feedback to the arduino service
                fs.setTrue();
                System.out.println("hello world");
            }catch(Exception e){
                System.out.println("task failed");
                e.printStackTrace();
            }
        }}
    }
}

尝试运行我的Web服务会导致它抛出以下异常:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为&#39; feedController&#39;的bean时出错在文件中定义   [d:\ Rishit \ Java的   工作空间\ FeedNemo \目标\类\控制器\ FeedController.class]:   通过构造函数参数0表示的不满意依赖:否   为依赖[int]找到的类型[int]的限定bean:期望在   至少有1个bean有资格作为autowire候选者   依赖。依赖注释:{};嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   为依赖[int]找到的类型[int]的限定bean:期望在   至少有1个bean有资格作为autowire候选者   依赖。依赖注释:{} at   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)   〜[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)   〜[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)   〜[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)   [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)   [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:313)   [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)   [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at   org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)   [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at   com.example.DemoApplication.main(DemoApplication.java:17)   [classes /:na]引起:   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   为依赖[int]找到的类型[int]的限定bean:期望在   至少有1个bean有资格作为autowire候选者   依赖。依赖注释:{} at   org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)   〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] ... 19个常见帧   省略

但是,如果我删除&#34; ScheduledThreadPoolExecutor&#34;和构造函数,它运行良好。有人可以解释扩展课程有什么问题吗?

注意:

1)在下面提到的帖子中建议扩展课程作为我最初问题的解决方案。最初,我的runnable没有运行,没有任何通知或错误消息

2)以下帖子使用了辱骂语言。然而,这是唯一一个为我的初始问题提供解决方案的人。指出最初的问题只是为了清楚地说明为什么我扩展了上面的类并且可能与我当前的问题没有直接关系。如果您发现这种语言令人反感,请不要打开。

http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/

1 个答案:

答案 0 :(得分:-1)

不要使bean成员静态。他们从多个线程引用的事实是无关紧要的;确保依赖关系填充是Spring的工作。

此外,更喜欢构造注入到现场注入;它使这些问题的可能性大大降低。