Guru Ayyappa Guru Ayyappa - 3 months ago 48
Java Question

org.springframework.beans.factory.UnsatisfiedDependencyException , No qualifying bean of type [edu.sample.service.ItemService]

It is spring boot application. i am getting below exception.how resolve it please help me
My Controller class is

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 class is

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

}


And DAO class

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

}


I get this exception message:

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

Answer

You must to named ItemServiceImpl with your Qualifier "itemService" like this:

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