我是構建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/
你在哪裏定義構造函數需要的說法? –
經過檢查,看起來問題不在於您發佈的代碼中,因爲它指的是其他字段,可能是超類中的一個字段。轉換爲構造函數注入並且問題可能會消失。 – chrylis
@DaveNewton對不起,我沒有正確理解你的問題。構造函數只接受corePoolSize。在其他地方沒有定義其他參數。 –