0

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)}
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
Guru Ayyappa
  • 51
  • 1
  • 6

2 Answers2

1

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

@Service("itemService")
public class ItemServiceImpl implements ItemService {
    ...
David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37
1

By default, the name of spring bean is taken from class name like itemServiceImpl for ItemServiceImpl.

With following declaration, you have a bean of type ItemService with name itemServiceImpl.

@Service
public class ItemServiceImpl implements ItemService {
 /** ... */
}

However, since you're using @Qualifier(value="itemService"), you are trying to autowire bean by it's name. ItemController needs a bean with name itemService but the spring context doesn't have bean with that name and thus the UnsatisfiedDependencyException.

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

    @Autowired
    @Qualifier(value="itemService")
    private ItemService itemService;
    /* ... */
}

Now, to solve your problem, you can either give your ItemServiceImpl bean an explicit name like

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

Another way is to let spring autowire ItemService bean by it's type rather than name by removing the @Qualifier annotation from ItemController. This approach works fine and is simple unless there are more than one bean of type ItemService in spring context.

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125