java - 无法自动接线。找不到“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
解决方案
我认为问题在PetServiceMap
和之间PetService
。
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);
}
}
推荐阅读
- reactjs - 面临通过 React Router 从 App 组件传递状态(通过 api 加载)的问题
- macos - Brew 在 macOS 上安装 Elasticsearch 7.9.2 失败
- javascript - 如何在AngularJs中格式化数字?
- html - Border of parent div not binding to size of children
- cpu-architecture - 现代计算中的存储程序计算机
- javascript - 反应上下文打字稿问题
- python - 如果您在读取 csv 文件后 seek(0),则 line_num 属性不会重置。为什么会发生这种情况?
- python-3.x - FastAPI 通过 SqlAlchemy 处理多个数据库连接
- python - 从 url 响应(文本文件)解析 XML 错误,开始时带有 HTML 块
- c++ - 我正在尝试在 C++ 中使用运算符重载,但是当我去实现该函数时,它会提供此错误