Description:
Parameter 0 of constructor in tacos.web.DesignTacoController required a bean of type 'tacos.data.IngredientRepository' that could not be found.
Below controller;
package tacos.web;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.validation.Errors;import org.springframework.web.bind.annotation.*;import tacos.data.IngredientRepository;import tacos.model.Ingredient;import tacos.model.Ingredient.Type;import tacos.model.Taco;import tacos.model.TacoOrder;import javax.validation.Valid;import java.util.stream.Collectors;import java.util.stream.StreamSupport;@Slf4j@Controller@RequestMapping("/design")@SessionAttributes("tacoOrder")public class DesignTacoController { private final IngredientRepository ingredientRepo; @Autowired public DesignTacoController( IngredientRepository ingredientRepo) { this.ingredientRepo = ingredientRepo; } @ModelAttribute public void addIngredientsToModel(Model model) { Iterable<Ingredient> ingredients = ingredientRepo.findAll(); Type[] types = Ingredient.Type.values(); for (Type type : types) { model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type)); } } @ModelAttribute(name = "tacoOrder") public TacoOrder order() { return new TacoOrder(); } @ModelAttribute(name = "taco") public Taco taco() { return new Taco(); } @GetMapping public String showDesignForm() { return "design"; } @PostMapping public String processTaco( @Valid Taco taco, Errors errors, @ModelAttribute TacoOrder tacoOrder) { if (errors.hasErrors()) { return "design"; } tacoOrder.addTaco(taco); log.info("Processing taco: {}", taco); return "redirect:/orders/current"; } private Iterable<Ingredient> filterByType( Iterable<Ingredient> ingredients, Type type) { return StreamSupport.stream(ingredients.spliterator(), false) .filter(x -> x.getType().equals(type)) .collect(Collectors.toList()); }}
Below Service Class;
package tacos.data;import org.springframework.data.repository.CrudRepository;import org.springframework.stereotype.Repository;import tacos.model.Ingredient;@Repositorypublic interface IngredientRepository extends CrudRepository<Ingredient, String> {}
Below run class
package tacos;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import tacos.data.IngredientRepository;import tacos.model.Ingredient;import tacos.model.Ingredient.*;@SpringBootApplicationpublic class TacoCloudApplication { public static void main(String[] args) { SpringApplication.run(TacoCloudApplication.class, args); } @Bean public CommandLineRunner dataLoader(IngredientRepository repo) { return args -> { repo.deleteAll(); repo.save(new Ingredient("FLTO", "Flour Tortilla", Type.WRAP)); repo.save(new Ingredient("COTO", "Corn Tortilla", Type.WRAP)); repo.save(new Ingredient("GRBF", "Ground Beef", Type.PROTEIN)); repo.save(new Ingredient("CARN", "Carnitas", Type.PROTEIN)); repo.save(new Ingredient("TMTO", "Diced Tomatoes", Type.VEGGIES)); repo.save(new Ingredient("LETC", "Lettuce", Type.VEGGIES)); repo.save(new Ingredient("CHED", "Cheddar", Type.CHEESE)); repo.save(new Ingredient("JACK", "Monterrey Jack", Type.CHEESE)); repo.save(new Ingredient("SLSA", "Salsa", Type.SAUCE)); repo.save(new Ingredient("SRCR", "Sour Cream", Type.SAUCE)); }; }}
Exeption:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'tacos.data.IngredientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Parameter 0 of constructor in tacos.web.DesignTacoController required a bean of type 'tacos.data.IngredientRepository' that could not be found.
Consider defining a bean of type 'tacos.data.IngredientRepository' in your configuration.
I do not understand everything stands normal by its packages and annotations are everywhere, I do not understand what is the problem!