当前位置: 首页 > 知识库问答 >
问题:

无法自动连接。找不到“PetService”类型的bean

郭德惠
2023-03-14

我不确定我的代码出了什么问题。我正在学习弹簧靴。但我无法运行应用程序,因为我得到以下错误

模型类

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

共有1个答案

赫连宏伯
2023-03-14

我认为问题出在PetServiceMapPetService之间。PetServiceMap@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);
        }
}
 类似资料:
  • 无法自动连接。找不到“CustomAuthenticationSuccessHandler”类型的bean

  • 我在创建带有参数的@Bean时遇到问题,这运行得很好,但在intelliJ中,它给出的错误无法自动连线。找不到“String”类型的bean。 我要做什么?我正在尝试创建具有原型作用域的bean,在IntelliJ“无法自动连线。未找到‘String’类型的bean”中出现此错误有谁能帮我解决这个问题吗 这是原型测试的类

  • 我使用的是Spring Data Jpa,这是我的项目结构: 下面是: 以下是: 我使用将注入到,但IntelliJ总是抱怨 无法自动连接。找不到“MyRepository”类型的bean 即使代码能够成功编译并运行。 为什么IntelliJ不能识别这不是一个错误?如何解除Intellij的警告? IntelliJ版本:2018.2.6

  • 试图在SpringBoot应用程序中创建bean,但出现以下错误“无法自动连线。找不到“InstructionRepository”类型的bean” InstructionRepository在jar中用@Repository注解,是一个扩展Spring数据接口的接口 ScheduleProcessor是一种方法 当我尝试通过传递基本包值来添加@ComponentScan注释时,错误消失了,但是,

  • 我想使用Jooq在postgres数据库中插入数据这是我的服务类 但是我有这个错误 无法自动连线。找不到“DSLContext”类型的bean。

  • 我想在测试中使用WebTestClient。工作原理如下: 但现在我想将WebTestClient注入到一个helper类中: 下面是一个有问题的测试项目:https://github.com/kicktipp/demo 如何在Helper类上使用WebTestClient?