Partage
  • Partager sur Facebook
  • Partager sur Twitter

Spring Boot - helloworld - problème autowired

Sujet résolu
    22 juillet 2021 à 16:36:30

    Bonjour,

    J'essaye d'apprendre le fonctionnement de Spring en suivant le cours "Créez une application Java avec Spring Boot".

    Cependant je rencontre un problème lors de l'étape " Écrivez votre premier Hello World":

    Lorsque je Run mon application avec IntelliJ j'obtiens le message suivant:

    "

     :: Spring Boot ::                (v2.5.0)

    2021-07-22 16:16:08.362  INFO 2540 --- [           main] c.e.H.HelloTheWorldApplication           : Starting HelloTheWorldApplication using Java 16.0.1 on User-PC with PID 2540 (C:\Users\User\Desktop\HelloTheWorld\target\classes started by User in C:\Users\User\Desktop\HelloTheWorld)

    2021-07-22 16:16:08.365  INFO 2540 --- [           main] c.e.H.HelloTheWorldApplication           : No active profile set, falling back to default profiles: default

    2021-07-22 16:16:08.727 ERROR 2540 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

    ***************************

    APPLICATION FAILED TO START

    ***************************

    Description:

    Field bs in com.example.HelloTheWorld.HelloTheWorldApplication required a bean of type 'com.openclassrooms.helloworld.service.BusinessService' 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.openclassrooms.helloworld.service.BusinessService' in your configuration.

    Process finished with exit code 1"

    J'ai pourtant bien préciser que la classe BusinessService est un bean à l'aide de l'annotation @Component

    BusinessService.Java:

    package com.openclassrooms.helloworld.service;
    
    import com.openclassrooms.helloworld.model.HelloWorld;
    import org.springframework.stereotype.Component;
    
    @Component
    public class BusinessService {
    
        public HelloWorld getHelloWorld(){
            HelloWorld hw = new HelloWorld();
            return hw;
        }
    }
    Cependant dans la page "HelloTheWorldApplication.java" bs reste souligné en rouge et m'indique que "Could not autowire. No beans of 'BusinessService' type found"
    package com.example.HelloTheWorld;
    
    import com.openclassrooms.helloworld.model.HelloWorld;
    
    import com.openclassrooms.helloworld.service.BusinessService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HelloTheWorldApplication implements CommandLineRunner {
    
    	@Autowired
    	private BusinessService bs;
    
    	public static void main(String[] args) {
    		SpringApplication.run(HelloTheWorldApplication.class, args);
    	}
    
    	@Override
    	public void run(String... args) throws Exception {
    
    		HelloWorld hw = bs.getHelloWorld();
    		System.out.println(hw);
    	}
    
    }

    -
    Edité par Tenkai188 22 juillet 2021 à 16:37:48

    • Partager sur Facebook
    • Partager sur Twitter
      24 juillet 2021 à 2:45:28

      Spring Boot n'inspecte pas absolument toutes les classes de l'univers pour voir si elles sont annotées.

      L'annotation @SpringBootApplication peut prendre en paramètre une liste de packages dans lesquels chercher des classes annotées (et ça inclura leurs sous-packages).

      Si on n'indique rien, par défaut, il cherchera dans le même package (et ses sous-packages) qui a @SpringBootApplication.

      Donc, toi tu as ta classe HelloTheWorldApplication  dans com.example.HelloTheWorld, et ta classe BusinessService  dans com.openclassrooms.helloworld.service .

      C'est pas dans la même branche de packages, c'est pour ça qu'il le trouve pas.

      • Partager sur Facebook
      • Partager sur Twitter
        25 juillet 2021 à 15:35:49

        ok problème résolu

        Merci pour ton aide :)

        Je passe en résolu !

        • Partager sur Facebook
        • Partager sur Twitter
          18 septembre 2021 à 23:02:23

          @Tenkaii, comment as tu resolu le probleme?
          • Partager sur Facebook
          • Partager sur Twitter
            20 septembre 2021 à 15:35:45

            Probablement en faisant en sorte que l'erreur que j'ai expliquée ne soit plus là, non ?
            • Partager sur Facebook
            • Partager sur Twitter
              21 septembre 2021 à 9:26:48

              Bonjour,

              @Kumesana Merci pour ta réponse mais inutile d'être condescendant. Nous sommes en train d'apprendre...

              @DilaneKamga Il faut écrire dans HelloWorldApplication

              @SpringBootApplication(scanBasePackages = {"com.openclassroom.*"})
              ou
              @SpringBootApplication(scanBasePackages = {"com.openclassroom.service"})
              • Partager sur Facebook
              • Partager sur Twitter
                27 septembre 2021 à 14:31:59

                Mais en fait, on n'est pas sensé rajouter cela.

                C'est parce que tu as mal fait l'arborescence du projet.

                Par le fait qu'on doit rajouter cela: @SpringBootApplication(scanBasePackages = {"com.openclassroom.*"})

                Cela veut dire que tu as une arborescence comme cela:

                Mais il faut avoir une arborescence comme cela:

                • Partager sur Facebook
                • Partager sur Twitter

                Spring Boot - helloworld - problème autowired

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown