SpringBoot起動時エラー:Error creating bean with name'XXXX'
1978 ワード
Error creating bean with name 'XXXX'
完全なエラーは次のとおりです。 Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
'testHelloController': Unsatisfied dependency expressed through field 'testHelloService'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
'com.jun.service.test.TestHelloService' available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
原因分析:
SpringBoot初心者なので、まずプロジェクトを走らせて効果を見たいだけです.もし、半日かけてやっとプロジェクトの構造を完全に構築し、起動すると、このような間違いを報告します.原因はもうはっきり言っています.つまり、起動時にTestHelloServiceという対象テンプレートが見つからず、サービス層が注釈を打っているのを見て、頭が動いています.SpringBootが起動した時にサービス層をスキャンしていなかったのか、後で資料を見てみると、やはりSpringBootの起動クラスApplicationで直接注釈@ComponentScanを打つと、この問題を解決できます.以下のように、エラーを報告する前のコード:package com.jun.web.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
エラー発生後のコードを解決するには、次の手順に従います.package com.jun.web.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
**@ComponentScan(basePackages = {"com.jun.core.*","com.jun.service.*","com.jun.web.*"})**
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
'testHelloController': Unsatisfied dependency expressed through field 'testHelloService'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
'com.jun.service.test.TestHelloService' available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
SpringBoot初心者なので、まずプロジェクトを走らせて効果を見たいだけです.もし、半日かけてやっとプロジェクトの構造を完全に構築し、起動すると、このような間違いを報告します.原因はもうはっきり言っています.つまり、起動時にTestHelloServiceという対象テンプレートが見つからず、サービス層が注釈を打っているのを見て、頭が動いています.SpringBootが起動した時にサービス層をスキャンしていなかったのか、後で資料を見てみると、やはりSpringBootの起動クラスApplicationで直接注釈@ComponentScanを打つと、この問題を解決できます.以下のように、エラーを報告する前のコード:
package com.jun.web.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
エラー発生後のコードを解決するには、次の手順に従います.
package com.jun.web.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
**@ComponentScan(basePackages = {"com.jun.core.*","com.jun.service.*","com.jun.web.*"})**
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}