2016-08-03 8 views
1

私は春のブートWebサービスにmysqlデータベースを接続しようとしましたが、私はこのエラーを受け取りました。 。org.springframework.beans.factory.UnsatisfiedDependencyException:名前がuserControllerのBeanを作成中にエラーが発生しました:Unsatisfied dependency

@RestController 
    @RequestMapping("/user") 
    public class UsersController { 

    @Autowired 
    UserService userService; 

    @RequestMapping("/test") 
    @ResponseBody 
    String home(){ 
     return "Hello world"; 
    } 

    @RequestMapping("/users") 
    String test(){ 
     return "TEST "; 
    } 
} 

と私のエンティティは次のとおりです:

私のコントローラがある

@Entity 
public class User { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private int id; 

    private String username; 
    private String name; 
    private String firstname; 
    private String email; 
    private String adress; 
    private String password; 

    // Constructeur pour l'utilisation des JPA 
    protected User(){} 

    public User(String username, String name, String firstname, String email, String adress, String password) { 
     this.username = username; 
     this.name = name; 
     this.firstname = firstname; 
     this.email = email; 
     this.adress = adress; 
     this.password = password; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getFirstname() { 
     return firstname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getAdress() { 
     return adress; 
    } 

    public void setAdress(String adress) { 
     this.adress = adress; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 
} 

マイリポジトリファイル:

public interface UserRepository extends CrudRepository<User, Integer>{ 
    List<User> findUserByName(String name); 
    User findUserByUsername(String username); 
} 

マイサービス:

@Service 
@Component 
public class UserService { 
    @Autowired 
    private UserRepository repoUser; 

    public List<User> getUser(String name){ 
     return repoUser.findUserByName(name); 
    } 
} 

はEDIT: これは私のスタックトレースです:

2016-08-05 20:48:39.025 WARN 1849 --- [   main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersController': Unsatisfied dependency expressed through field 'userService': No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
2016-08-05 20:48:39.026 INFO 1849 --- [   main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 
2016-08-05 20:48:39.031 INFO 1849 --- [   main] o.apache.catalina.core.StandardService : Stopping service Tomcat 
2016-08-05 20:48:39.053 INFO 1849 --- [   main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug) 


2016-08-05 20:48:39.073 ERROR 1849 --- [   main] o.s.boot.SpringApplication    : Application startup failed 

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersController': Unsatisfied dependency expressed through field 'userService': No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at 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] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at 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(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] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at 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.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 
    at com.example.JitBackofficeApplication.main(JitBackofficeApplication.java:13) [classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101] 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na] 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
    ... 24 common frames omitted 

マイaplication.properties:

spring.datasource.url=jdbc:mysql://localhost/jitdatabase 
spring.datasource.username=root 
spring.datasource.password=root 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.data.jpa.repositories.enabled=true 

ここに私のアプリケーションファイル:

​​

here theres is my architecture

誰もが解決策を持っていますか?

よろしく

+0

どこにエラーがありますか? – andre3wap

+0

私はuserServiceをautowiredできないので、エラーはusercontrollerにあります:/ – tchekroun

+0

あなたのクラスはどのパッケージにありますか? –

答えて

2

@ComponentScan注釈が間違っています。コントローラパッケージはコンポーネントソースとしてのみ追加します。

コントローラとサービスの両方を含むパッケージを追加する必要があります。

例:あなたのパッケージ構造がある場合:

com.mycompany.controllers

com.mycompany。サービス

次に、com.mycompanyを@ComponentScanに書き込みます。

@Serviceもサービスクラスでのみ使用し、@Componentでは使用しないでください。

+0

私のcomponentScanには何が必要ですか? – tchekroun

+1

ありがとうございました! – tchekroun

0

私は、前にこの問題を持って、私はそれをトレースし、これは、コントローラおよびサービスのエラーではないことがわかったが、repository.Iは私が見つけたxxx'.Finallyによって引き起こされる例外スタック」によってこれを得ましたその理由は、データソースをapplication.propertiesにコンフィグレーションするときに間違いを犯し、データベースへの接続に失敗することになります。

あなたのエラーは不完全です。あなたのエラーは私のようだと私は確認するためにいくつかの質問があると思います。

1.完全な例外スタックを表示できますか?

2.あなたのapplication.propertiesを表示できますか?

3.データベースが正常に接続できることを確認しますか?jdbcを使用してテストできますか。

4. 1つ以上のデータソースがあるとしたら、1つのデータソースが複雑になり、エラーが発生する可能性が高くなります。

+0

私の質問、私のデータベースの作業を編集し、私はまだ私のWebサービスが動作しない理由を理解していない...私は問題が私のエンティティまたはリポジトリについてだと思う – tchekroun

+0

例外スタックを見た、それは@ComponentScanあなたのサービスをスキャンしないでください。あなたのJitBackofficeApplicationクラスを表示することができますか?ComponentScanが正しく構成されているかどうかを確認していますか? @ComponentScan( "boot.web、boot.service、boot.repository、boot.app" ) と私のディレクトリ構造があるが、以下:/ -----boot.app -----------App.java メイン のsrc/----- boot.domain - ---- boot.repository ----- boot.service ----- bo ot.web App.javaは私のスタートアップクラスです。 –

+0

私は自分の投稿を編集しています(私は私のアーキテクチャを見るために写真を持っていましたが、私は本当にcomponentScanを理解していません... – tchekroun

関連する問題