2016-08-21 92 views
0

我是构建Web服务的新手,我开始使用spring引导来构建一个。我创建了以下控制器类扩展类抛出UnsatisfiedDependencyException异常

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: 误差名创建豆“文件 [D:\ Rishit \ Java workspaces \ FeedNemo \ target \ classes \ controller \ FeedController.class]中定义的feedController': 通过构造函数参数表示的不完全依赖0:否 依赖ency [int]:预计在 至少1个符合此 依赖关系的自动连线候选资格。依赖注释:{};嵌套的异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 为依赖性[int]找到的[int]类型符合条件的bean:预计在 至少1个符合此 依赖关系的候选资格的bean。依赖注释:{} 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) 〜[弹簧豆-4.3.2.RELEASE.jar:4.3.2.RELEASE]在 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) 〜[弹簧豆-4.3.2.RELEASE.jar:4.3.2.RELEASE]在 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.springfram ework.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(AbstractApplica tionContext.java:541) 〜[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext。刷新(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]在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [弹簧引导1.4.0.RELEASE.jar:1.4 .0.RELEASE] 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) [弹簧引导1.4.0.RELEASE.jar:1.4.0.RELEASE]在 org.springframework.boot.SpringApplication.run(SpringApplication.j ava: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个符合此 依赖项的自动装填候选项。依赖注释:{} 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个常用帧 省略

但是,如果我删除了“ScheduledThreadPoolExecutor”和构造函数,它运行良好。有人能解释扩展课程有什么问题吗?

注:

1)扩展类建议在下文提到的岗位作为解决我最初的问题。起初,我可运行未运行没有任何类型的通知或错误信息

2)后下利用粗言秽语的。然而,这是唯一为我最初的问题提供解决方案的人。最初的问题只是为了清楚说明为什么我扩展上述课程,并且可能与我目前的问题没有直接关系。如果您发现这种语言令人不快,请不要打开。

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

+0

你在哪里定义构造函数需要的说法? –

+0

经过检查,看起来问题不在于您发布的代码中,因为它指的是其他字段,可能是超类中的一个字段。转换为构造函数注入并且问题可能会消失。 – chrylis

+0

@DaveNewton对不起,我没有正确理解你的问题。构造函数只接受corePoolSize。在其他地方没有定义其他参数。 –

回答

-1

不要让豆成员静态的。它们从多个线程引用的事实是不相关的;这是Spring的工作,以确保依赖项被填充。

另外,喜欢构造函数注入到字段注入;它使这类问题显着减少。