質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

4372閲覧

autowiredでinterfaceをアノテーションした際、interfaceをBeanで定義してくださいとコンパイルエラーになる。

退会済みユーザー

退会済みユーザー

総合スコア0

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2018/08/15 11:12

log

1org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.home.service.HomeInterface' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 3 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 4 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 5 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 6 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 7 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 8 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1350) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 9 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:580) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 10 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 11 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 12 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 13 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 14 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 15 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] 16 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE] 17 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE] 18 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 19 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 20 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 21 at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 22 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 23 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE] 24 at com.Application.main(Application.java:13) [main/:na] 25 262018-08-15 20:01:09.676 ERROR 12524 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : 27 28*************************** 29APPLICATION FAILED TO START 30*************************** 31 32Description: 33 34Field service in com.home.controller.HomeController required a bean of type 'com.home.service.HomeInterface' that could not be found. 35 36 37Action: 38 39Consider defining a bean of type 'com.home.service.HomeInterface' in your configuration.

java

1package com; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5/* 6 * mainメゾット 7 * 読み込み先をcontrollerパッケージへ指定する。 8 */ 9@SpringBootApplication(scanBasePackages ={"com.login.controller","com.home.controller"}) 10public class Application{ 11 12 public static void main(String[] args) { 13 SpringApplication.run(Application.class, args); 14 } 15 16}

HomeController

1package com.home.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.context.annotation.ComponentScan; 5import org.springframework.stereotype.Controller; 6import org.springframework.web.bind.annotation.RequestMapping; 7import org.springframework.web.bind.annotation.RequestMethod; 8import org.springframework.web.servlet.ModelAndView; 9 10import com.form.FormLogin; 11import com.home.service.HomeInterface; 12 13/* 14 * ThirdCore ver1.0 15 * ホーム画面 16 */ 17@Controller 18@ComponentScan("com.form") 19@ComponentScan("com.home.service") 20public class HomeController { 21 22 @Autowired 23 public HomeInterface service; 24 @Autowired 25 public FormLogin form; 26 27 ModelAndView model = new ModelAndView(); 28 29 /* 30 *ログイン画面(初期) 31 *profile欄の読み込み 32 */ 33 @RequestMapping(value = "/login/home", method = RequestMethod.GET) 34 public Object home() { 35 model.setViewName("home.html"); 36 return service.homesyoki(form); 37 } 38}

HomeInterface

1package com.home.service; 2 3import org.springframework.stereotype.Service; 4 5import com.form.FormLogin; 6 7public interface HomeInterface { 8 public Object homesyoki(FormLogin form); 9} 10

LoginContorller

1package com.login.controller; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.beans.propertyeditors.StringTrimmerEditor; 5import org.springframework.context.annotation.ComponentScan; 6import org.springframework.stereotype.Controller; 7import org.springframework.web.bind.WebDataBinder; 8import org.springframework.web.bind.annotation.InitBinder; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.RequestMapping; 11import org.springframework.web.bind.annotation.RequestMethod; 12import org.springframework.web.servlet.ModelAndView; 13import com.form.FormLogin; 14import com.login.service.LoginInterface; 15 16/* 17 * Controllerクラス 18 * 19 * <遷移先> 20 * ログイン画面⇔登録画面 21 * ログイン画面⇒ホーム画面 22 * 23 * @version ThridCore ver1.0 24 * @author Rei.Yamaguchi 25 */ 26@Controller 27@ComponentScan("com.login.service") 28@ComponentScan("com.form") 29public class LoginController { 30 /* 31 * 自動マッピング先 32 * @param serivce com.login.LoginServiceクラスのインスタンスが含まれている 33 * @param form com.from.FormLoginクラスのインスタンスが含まれている 34 */ 35 @Autowired 36 public LoginInterface service; 37 @Autowired 38 public FormLogin form; 39 //ModelAndViewの統一化 40 ModelAndView model = new ModelAndView(); 41 42 /* 43 * フォームにnullの投入 44 */ 45 @InitBinder 46 public void initbinder(WebDataBinder binder) { 47 binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); 48 } 49 50 /* 51 *ログイン画面(初期) 52 *@return model 53 */ 54 @RequestMapping(value = "/login", method = RequestMethod.GET) 55 public Object login() { 56 model.addObject("isEven", false); 57 model.setViewName("login.html"); 58 return model; 59 } 60 61 /* 62 *アカウント作成画面(初期) 63 *@return model 64 */ 65 @RequestMapping(value = "new", method = RequestMethod.GET) 66 public Object NewAccount() { 67 //model.addObject("isEven", false); 68 model.setViewName("login.html"); 69 return model; 70 } 71 72 /* 73 *ログイン画面(ログインイベント) 74 *return 成功時home画面 失敗時login画面 75 */ 76 @RequestMapping(value = "login", method = RequestMethod.POST) 77 public Object login(@ModelAttribute FormLogin form) { 78 return service.Shyouzi(form); 79 } 80 81 /* 82 * 新規アカウント作成画面(アカウント作成イベント時) 83 * return 成功時 アカウント作成画面 失敗時 アカウント作成画面(失敗表示) 84 */ 85 @RequestMapping(value = "new", method = RequestMethod.POST) 86 public Object NewAccount(@ModelAttribute FormLogin form) { 87 88 return service.Snew(form); 89 } 90}

LoginInterface

1package com.login.service; 2 3import com.form.FormLogin; 4 5 6public interface LoginInterface { 7 public Object Shyouzi(FormLogin from); 8 public Object Snew(FormLogin from); 9}

applicationproperties

1spring.datasource.url=jdbc:mysql://localhost:3306?autoReconnect=true&useSSL=false 2spring.datasource.username=admin 3spring.datasource.password=admin 4spring.datasource.driver-class-name=com.mysql.jdbc.Driver 5debug=true 6spring.thymeleaf.mode=HTML

試した結果
scanBasePackages ={"com.login.controller"}だけだと正常に動作する
scanBasePackages ={"com.login.controller","com.home.controller"}←これだとエラーになる
scanBasePackages ={"com.home.controller"}←これだとエラーになる

interfaceに@serivceをつけてみる×

以上、よろしくお願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

HomeInterfaceの実装クラスがBean定義されていないのではないでしょうか?
@Autowiredでインジェクトする場合は、少なくとも1つの実装クラスが必要です。

投稿2018/08/15 11:30

d-yosh

総合スコア270

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問