Bonjour,

je tente de reprendre mes TP petits à petit, mais j'ai un problème dans mon contrôleur lorsque je tente de lier mon service

L'erreur est la suivante:

Could not autowire. No beans of 'ProduitService' type found. less... (Ctrl+F1)
Inspection info:Checks autowiring problems in a bean class.
Et quand on essaie de lancer tout de même l'api avec l'erreur:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'produitController': Unsatisfied dependency expressed through field 'ps'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'services.ProduitService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Ma classe domaine

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Entity
public class Produit {
 
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    Long id;
 
    @NotNull
    String nom;
 
    public Produit(){
 
    }
 
    public Produit(String nom){
        this.nom=nom;
    }
}

Mon repository

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
public interface ProduitRepository extends CrudRepository<Produit,Long> {
    Produit findById(long id);
    Produit save(Produit p);
    ArrayList<Produit> findAll();
 
}
Mon service (j'ai volontairement effectué 0 opérations de contrôle dans le service ici pour ne pas alourdir le code ce n'est pas l'objectif).

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Service
public class ProduitService {
 
    @Autowired
    private ProduitRepository repoP;
 
    public ProduitService(){
 
    }
 
    public Produit saveProduit(Produit p){
        return repoP.save(p);
    }
 
    public Produit getProduitById(long id){
        return repoP.findById(id);
    }
 
    public ArrayList<Produit> getProduits(){
        return repoP.findAll();
    }
 
}
Le controller

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@RestController
public class ProduitController implements ErrorController {
 
 
 
    @Autowired
    private ProduitService ps;
 
    public ProduitService getPs() {
        return ps;
    }
 
    public void setPs(ProduitService ps) {
        this.ps = ps;
    }
 
    @RequestMapping("/")
    public String index() {
        return "page index";
    }
 
    @RequestMapping("/error")
    public String error() {
        return "gestion erreur";
    }
 
    @GetMapping("/produit/{id}")
    public Produit getProduitById(@PathVariable Long id){
        return ps.getProduitById(id);
    }
 
    @GetMapping("/produits")
    public ArrayList<Produit> getProduits(){
        return ps.getProduits();
    }
 
    @GetMapping("/produit/new")
    public Produit newProduit(){
        return ps.saveProduit(new Produit("Test product"));
    }
 
 
    @Override
    public String getErrorPath() {
        return "/error";
    }
}