首页 > 解决方案 > 无法自动接线。找不到“PetService”类型的 bean

问题描述

我不确定我的代码有什么问题。我正在尝试学习 Spring Boot。但我无法运行应用程序,因为我收到以下错误

com.springframework.sfgpetclinic2.services.map.OwnerServiceMap 中构造函数的参数 1 需要找不到类型为“com.springframework.sfgpetclinic2.services.PetService”的 bean。

尝试了 @Autowired 注释,但它不起作用。在 IntelliJ 中,它给出了无法自动装配。找不到“PetService”类型的 bean。

模型类

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;
    }}

主要课程:

@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

标签: javaspringspring-bootdependency-injectionintellij-14

解决方案


我认为问题在PetServiceMap和之间PetServicePetServiceMap被注释@Service但它没有实现PetService.

解决方案:

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);
        }
}

推荐阅读