1
votes

I am not sure what is wrong with my code. I am trying to learn Spring Boot. But I am not able to run the application as I get the below error

Parameter 1 of constructor in com.springframework.sfgpetclinic2.services.map.OwnerServiceMap required a bean of type 'com.springframework.sfgpetclinic2.services.PetService' that could not be found.

Tried @Autowired annotation but it is not working. In IntelliJ it is giving Could not autowire. No beans of 'PetService' type found.

MODEL CLASSS

public class Pet extends BaseEntity{    private PetType pettype;
    private Owner owner;
    private LocalDate date;
    private String name;

    public String getName() {
        return name;
    }

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

    public PetType getPettype() {
        return pettype;
    }
    public void setPettype(PetType pettype) {
        this.pettype = pettype;
    }
    public Owner getOwner() {
        return owner;
    }
    public void setOwner(Owner owner) {
        this.owner = owner;
    }
    public LocalDate getDate() {
        return date;
    }
    public void setDate(LocalDate date) {
        this.date = date;
    }

}
public interface PetService extends CrudService<Pet,Long> { 

}
import java.util.Set;

import com.springframework.sfgpetclinic2.model.Pet;
import com.springframework.sfgpetclinic2.services.CrudService;
import org.springframework.stereotype.Service;

@Service
public class PetServiceMap extends AbstractMapService<Pet,Long> implements CrudService<Pet,Long> {

     @Override
        public Set<Pet> findAll() {
            return super.findAll();
        }

        @Override
        public Pet findById(Long id) {
            return super.findById(id);
        }

        @Override
        public Pet save(Pet object) {
            return super.save(object);
        }

        @Override
        public void delete(Pet object) {
            super.delete(object);
        }

        @Override
        public void deleteById(Long id) {
            super.deleteById(id);
        }
}

import java.util.Set;
@Service
public class OwnerServiceMap extends AbstractMapService<Owner,Long> implements OwnerService {

    private final PetTypeService petTypeService;
    private final PetService petService;


    public OwnerServiceMap(PetTypeService petTypeService, PetService petService) {
        this.petTypeService = petTypeService;
        this.petService = petService;
    }

    @Override
        public Set<Owner> findAll() {
            return super.findAll();
        }

        @Override
        public Owner findById(Long id) {
            return super.findById(id);
        }

        @Override
        public Owner save(Owner object) {
            if(object!=null)
            {
                if(object.getPets()!=null){
                    object.getPets().forEach(pet -> {
                        if(pet.getPettype()!=null){
                            if(pet.getPettype().getId()==null)
                            {
                                pet.setPettype(petTypeService.save(pet.getPettype()));
                            }
                        }
                        else
                        {
                            throw new RuntimeException("Pet Type is Required");
                        }
                        if(pet.getId()==null)
                        {
                            Pet savedPet=petService.save(pet);
                            pet.setId(savedPet.getId());
                        }
                    });
                }
                return super.save(object);
            }
            else {
                return null;
            }

        }

        @Override
        public void delete(Owner object) {
            super.delete(object);
        }

        @Override
        public void deleteById(Long id) {
            super.deleteById(id);
        }


    @Override
    public Owner findByLastName(String lastName) {
        return null;
    }}

MAIN CLASS:

@SpringBootApplication
public class SfgPetClinic2Application {

    public static void main(String[] args) {
        SpringApplication.run(SfgPetClinic2Application.class, args);
    }
}
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-14 21:38:08.099 ERROR 30760 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in com.springframework.sfgpetclinic2.services.map.OwnerServiceMap required a bean of type 'com.springframework.sfgpetclinic2.services.PetService' that could not be found.
Action:
Consider defining a bean of type 'com.springframework.sfgpetclinic2.services.PetService' in your configuration.
Process finished with exit code 0
1
Have you considered defining a bean of type PetService in your configuration?Sotirios Delimanolis
No, I was learning Spring boot through a Udemy course and following the instructor.rishav_agarwal

1 Answers

1
votes

I think the problem is between PetServiceMap and PetService. PetServiceMap is annotated by @Service but it does not implement PetService.

Solution:

import java.util.Set;

import com.springframework.sfgpetclinic2.model.Pet;
import com.springframework.sfgpetclinic2.services.CrudService;
import org.springframework.stereotype.Service;

@Service
public class PetServiceMap extends AbstractMapService<Pet,Long> implements PetService {

     @Override
        public Set<Pet> findAll() {
            return super.findAll();
        }

        @Override
        public Pet findById(Long id) {
            return super.findById(id);
        }

        @Override
        public Pet save(Pet object) {
            return super.save(object);
        }

        @Override
        public void delete(Pet object) {
            super.delete(object);
        }

        @Override
        public void deleteById(Long id) {
            super.deleteById(id);
        }
}