我是 Build 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:在文件[D:\ Rishit \ Java workspaces \ FeedNemo \ target \ classes \ controller \ FeedController.class]中定义名称为'feedController'的bean创建错误:通过构造函数参数表示的不满意依赖项0:没有为依赖项[int]找到类型为[int]的限定bean:预期至少有1个bean可以作为此依赖项的autowire候选者 . 依赖注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项[int]的[int]类型的限定bean:期望至少有1个bean符合此依赖项的autowire候选者 . 依赖注释:{}在org.springframework的org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] orbe.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory中的.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] . orow.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)中的autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]〜 [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]在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.AbstractAutowireCapableBeanFa org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:ctory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 306)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 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] org.springframework上的org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] . bean.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] org.springframework.context.support.AbstractApplicationContext.finishBea nFactoryInitialization(AbstractApplicationContext.java:861)〜[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE] 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]在org.springframework.boot上的org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] .SpringApplication.refreshContext(SpringApplication.java:369)[spring-boot-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]在org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)[spring-boot-1.4.0.RELEASE.jar:1.4.0 . 发布在org.springframework.boot.SpringApplication.r上un(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:预期至少有一个bean可以作为此依赖项的autowire候选者 . 依赖注释:{} atorg.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] org.springframework.beans.factory.support .DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java: 1019)〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 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)在下面提到的帖子中建议扩展课程作为我最初问题的解决方案 . 最初,我的runnable没有运行没有任何通知或错误消息
2)以下帖子使用辱骂语言 . 然而,这是唯一一个为我的初始问题提供解决方案的人 . 指出最初的问题只是为了清楚说明我扩展上述类的原因,并且可能与我当前的问题没有直接关系 . 如果您发现这种语言令人反感,请不要打开 .
http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/
1 回答
不要让bean成员静态 . 他们从多个线程引用的事实是无关紧要的; Spring的工作是确保填充依赖项 .
此外,更喜欢构造注入到现场注入;它使这些问题的可能性大大降低 .