gpt4 book ai didi

java - org.springframework.beans.factory.UnsatisfiedDependencyException ,没有类型 [edu.sample.service.ItemService] 的合格 bean

转载 作者:行者123 更新时间:2023-12-02 03:24:22 26 4
gpt4 key购买 nike

这是 Spring Boot 应用程序。我遇到了异常。如何解决它请帮助我 我的 Controller 类是

import edu.sample.model.Item;
import edu.sample.service.ItemService;

@ComponentScan(basePackages = "edu.*")
@RestController
public class ItemController {

@Autowired
@Qualifier(value="itemService")
private ItemService itemService;

@RequestMapping(value = "/getAllItems", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Item>> getAllItems() {
List<Item> items = itemService.getAllItems();
return new ResponseEntity<List<Item>>(items, HttpStatus.OK);
}
}

服务类别是

@Service
public class ItemServiceImpl implements ItemService {

@Autowired
ItemDAO itemDAO;

@Override
public String addItem(Item item) {
return itemDAO.addItem(item);
}

@Override
public String deleteItem(Integer id) {
return itemDAO.deleteItem(id);
}

@Override
public void updateItem(Item item) {

}

@Override
public List<Item> getAllItems() {
return itemDAO.getAllItems();
}

}

和 DAO 类

@Resource
public class ItemDAOImpl implements ItemDAO {

@PersistenceContext
EntityManager entityManager;

@Override
public String addItem(Item item) {
entityManager.persist(item);
return item.getName();
}

@Override
public String deleteItem(Integer id) {
String name=entityManager.find(Item.class, id).getName();
entityManager.remove(id);
return name;
}

@Override
public void updateItem(Item item) {

}

@Override
public List<Item> getAllItems() {
return entityManager.createQuery("from Item").getResultList();
}

}

我收到此异常消息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController': Unsatisfied dependency expressed through field 'itemService': No qualifying bean of type [edu.sample.service.ItemService] found for dependency [edu.sample.service.ItemService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=itemService)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [edu.sample.service.ItemService] found for dependency [edu.sample.service.ItemService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=itemService)}

最佳答案

您必须使用限定符 "itemService" 来命名 ItemServiceImpl,如下所示:

@Service("itemService")
public class ItemServiceImpl implements ItemService {
...

关于java - org.springframework.beans.factory.UnsatisfiedDependencyException ,没有类型 [edu.sample.service.ItemService] 的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39173147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com