私はWebサービスを構築するのが初めてで、ビルドにはスプリングブートを使い始めました。クラスを拡張するとUnsatisfiedDependencyException例外がスローされる
org.springframework.beans.factory.UnsatisfiedDependencyException: エラーが名前のBeanを作成する「私は自分のWebサービスを実行しようとすると
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(); } }} } }
が、それは次の例外をスローするようになり、次のコントローラクラスを作成しました[D:\ Rishit \ Java workspaces \ FeedNemo \ target \ classes \ controller \ FeedController.class]に定義されている「feedController」: コンストラクタパラメータ0で表されている満足度の低い依存性:いいえ [int] ency [int]:この 依存のautowire候補となる 少なくとも1つのbeanに期待しています。依存関係の注釈:{};入れ子になっている例外は org.springframework.beans.factory.NoSuchBeanDefinitionException:いいえ [int]タイプの[int]の修飾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) 〜[スプリング豆-4.3.2.RELEASE.jar:4.3.2.RELEASE] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AT 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で。 (SpringAnalyzerCapableBeanFactory.java:482) にある[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 〜org.springframework.beans.factory.support.AbstractBeanFactory $ 1。 getObject(AbstractBeanFactory.java: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.springfram ework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] にあるorg.springframework.beans.factory.support .AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) で 〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]。org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory。 java:776) 〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] にある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〜[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]、 org.springframework.boot.context.embedded.EmbeddedWebApplicationContextにあります。(SpringApplication.java:759) [spring (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] 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.j ava:1174) で[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] com.example.DemoApplication.main(DemoApplication.java:17) [classes /:na]原因: org.springframework.beans.factory.NoSuchBeanDefinitionException:いいえ 依存のためのautowire候補となる 少なくとも1つのbeanで期待されている依存[int]のタイプ[int]の修飾子が で見つかりません。依存関係の注釈:{} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) 〜[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at (スプリングリスト)。 .desktop.BeanFactory.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] 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/
コンストラクタが期待する引数はどこで定義しますか? –
調査の結果、投稿されたコードに問題があるようには見えません。これはおそらくスーパークラスの1つの他のフィールドを参照しているからです。コンストラクタインジェクションに変換すると問題はおそらく消えます。 – chrylis
@DaveNewton申し訳ありませんが、私はあなたの質問を正しく理解していませんでした。コンストラクタはcorePoolSizeのみを取ります。それ以外の引数は他に定義されていません。 –