実現したいこと
技術評論社 SpringFramework超入門 第3章 のテキストの模写をしたところ、
エラーで実行できなくなってしまいました。
前提
DIコンテナのイメージをつかむため、テキスト記載通りにファイルを作成しましたが、以下のエラーとなってしまっております。
発生している問題・エラーメッセージ
*************************** APPLICATION FAILED TO START *************************** Description: Field greet in com.example.demo.DependencyInjectionSampleApplication required a bean of type 'com.example.demo.chapter03.used.Greet' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.example.demo.chapter03.used.Greet' in your configuration.
該当のソースコード
Java
1package com.example.demo.chapter03.used; 2 3/** 4* 挨拶インターフェース 5*/ 6public interface Greet { 7 /** 8 * 挨拶をする 9 */ 10 void greeting(); 11}
Java
1package com.example.demo.chapter03.used; 2 3import org.springframework.stereotype.Component; 4 5/** 6* Greet実装クラス<br> 7* 朝の挨拶を行う 8*/ 9 10@Component 11public class MorningGreet implements Greet { 12 @Override 13 public void greeting() { 14 System.out.println("------------------"); 15 System.out.println("おはようございます!"); 16 System.out.println("------------------"); 17 } 18}
Java
1package com.example.demo; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.boot.SpringApplication; 5import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7import com.example.demo.chapter03.used.Greet; 8 9/** 10* SpringBoot起動クラス 11*/ 12@SpringBootApplication 13public class DependencyInjectionSampleApplication { 14 /** 15 * mainメソッド 16 * @param args 17 */ 18 public static void main(String[] args) { 19 SpringApplication.run(DependencyInjectionSampleApplication.class, args) 20 .getBean(DependencyInjectionSampleApplication.class).execute(); 21 } 22 /** 23 * 注入される箇所(インターフェース) 24 */ 25 @Autowired 26 Greet greet; 27 /** 28 実行メソッド 29 */ 30 private void execute() { 31 greet.greeting(); 32 } 33}
試したこと
・ DependencyInjectionSampleApplication.javaをpackage com.example.demo.chapter03.used;と同じパッケージ配下に移動して実行
補足
テキストとeclipseのバージョンが違い、スタータ・プロジェクトを作成するときに以下テキストと差分があったこと。
テキスト: 型 Gradle
:実際 : タイプ Gradle -Groovy

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/02/26 01:37