Envisager de définir un bean de type " com.ensat.services.ProductService " dans votre configuration
J'ai
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@ComponentScan(basePackages = {"hello","com.ensat.controllers"})
@EntityScan("com.ensat.entities")
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
ProductController.java
package com.ensat.controllers;
import com.ensat.entities.Product;
import com.ensat.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Product controller.
*/
@Controller
public class ProductController {
private ProductService productService;
@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}
/**
* List all products.
*
* @param model
* @return
*/
@RequestMapping(value = "/products", method = RequestMethod.GET)
public String list(Model model) {
model.addAttribute("products", productService.listAllProducts());
System.out.println("Returning rpoducts:");
return "products";
}
/**
* View a specific product by its id.
*
* @param id
* @param model
* @return
*/
@RequestMapping("product/{id}")
public String showProduct(@PathVariable Integer id, Model model) {
model.addAttribute("product", productService.getProductById(id));
return "productshow";
}
//Afficher le formulaire de modification du Product
@RequestMapping("product/edit/{id}")
public String edit(@PathVariable Integer id, Model model) {
model.addAttribute("product", productService.getProductById(id));
return "productform";
}
/**
* New product.
*
* @param model
* @return
*/
@RequestMapping("product/new")
public String newProduct(Model model) {
model.addAttribute("product", new Product());
return "productform";
}
/**
* Save product to database.
*
* @param product
* @return
*/
@RequestMapping(value = "product", method = RequestMethod.POST)
public String saveProduct(Product product) {
productService.saveProduct(product);
return "redirect:/product/" + product.getId();
}
/**
* Delete product by its id.
*
* @param id
* @return
*/
@RequestMapping("product/delete/{id}")
public String delete(@PathVariable Integer id) {
productService.deleteProduct(id);
return "redirect:/products";
}
}
ProductService.java
package com.ensat.services;
import com.ensat.entities.Product;
public interface ProductService {
Iterable<Product> listAllProducts();
Product getProductById(Integer id);
Product saveProduct(Product product);
void deleteProduct(Integer id);
}
ProductServiceImpl.java
package com.ensat.services;
import com.ensat.entities.Product;
import com.ensat.repositories.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Product service implement.
*/
@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;
@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@Override
public Iterable<Product> listAllProducts() {
return productRepository.findAll();
}
@Override
public Product getProductById(Integer id) {
return productRepository.findOne(id);
}
@Override
public Product saveProduct(Product product) {
return productRepository.save(product);
}
@Override
public void deleteProduct(Integer id) {
productRepository.delete(id);
}
}
C'est mon erreur:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setProductService in com.ensat.controllers.ProductController required a bean of type 'com.ensat.services.ProductService' that could not be found.
Action:
Consider defining a bean of type 'com.ensat.services.ProductService' in your configuration.
J'en ai plein le journal: https://gist.github.com/donhuvy/b918e20eeeb7cbe3c4be4167d066f7fd
C'est mon code source complet
https://github.com/donhuvy/accounting/commit/319bf6bc47997ff996308c890eba81a6fa7f1a93
La façon de corriger l'erreur?
J'ai ajouté
ProductServiceImpl
dans la question puisque c'est le manque de haricot.
OriginalL'auteur Do Nhu Vy | 2016-11-06
Vous devez vous connecter pour publier un commentaire.
Le haricot n'est pas créé par le Printemps depuis
componentScan
attribut manque le paquet, oùProductServiceImpl
est situé.En outre,
@EnableJpaRepositories
est manquant. Donc, le Printemps ne peut pas le fil de votre référentiel.doit être remplacé par :
Il permettra de résoudre votre problème, mais cette manière de faire de défaites de la convention over configuration avantage de Printemps et le Printemps de Démarrage.
Si le
Application
bean classe est situé dans un package parent qui tous les autresclasses de haricots appartient ou à un sous-paquet, vous n'avez plus besoin de spécifier ces deux annotations :
dans le
@SpringBootApplication
classe.Par exemple, le déplacement
Application
dans lecom.ensat
paquet et de déplacer tous vos haricots dans ce package ou un enfant, il sera à la fois résoudre vos problèmes de configuration et d'alléger votre configuration.Pourquoi ?
Parce que le
@SpringBootApplication
comprend déjà (et plus) :Mais qui utilise le package de la classe actuelle comme
basePackage
valeur de découvrir les haricots/entités/dépôts, etc...La documentation se réfère sur ce point.
Ici :
Ici, il explique sur les entités de découverte fournie par
@EnableAutoConfiguration
77.3 Ressort référentiels de Données point :votre erreur est Causés par: org.springframework.les haricots.usine.NoSuchBeanDefinitionException: Pas de qualification bean trouvé pour la dépendance [com.ensat.les référentiels.ProductRepository]: attendre au moins 1 fève qui se qualifie comme autowire candidat. La dépendance des annotations: {}
merci, je sais que c'est un autre problème
J'ai mis à jour ma réponse à gérer le manque de référentiel. À essayer 🙂
Il fonctionne comme un charme!
OriginalL'auteur davidxxx
Je vivais cette erreur, et il m'a fallu une après-midi entière à le comprendre. J'ai vérifié tous les stackoverflow post, printemps.io docs, github question liée à ce problème, et aucune réponse ne pourrait résoudre ce qui n'allait pas.
En résumé, j'ai essayé:
Ce qui a fait le tour pour moi et ce que je vous suggère de vous faire si vous avez épuisé votre tente est
4.La Structure de votre colis afin qu'ils suivent les meilleures pratiques (il y a un bel article à propos de cette ici.)
Parfois, il peut être très douloureux pour déboguer ces applications. J'ai eu ma part. J'espère que cela aide!
OriginalL'auteur malvadao