yozawiratama
yozawiratama

Reputation: 4328

Spring Framework beans factory unsatisfied when I add new repository and service

I still new in spring framework. My client using spring framework + boot + swagger.

I tried to add new Dao as repository, like this :

package com.api.elastic.repository;

import java.util.List;

import com.api.elastic.entity.Terminal;

public interface TerminalDao {
    public boolean isExist(String p_index);
    public List<Terminal> findById();
    public List<Terminal> findAll();
    public List<String> getDataByDatabaseId(Integer id);

}

and after that I add service :

package com.api.elastic.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.api.elastic.entity.Terminal;
import com.api.elastic.repository.TerminalDao;

@Service
public class TerminalService {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    TerminalDao terminalDao;

    public boolean isExist(String p_index) {
        return terminalDao.isExist(p_index);
    }

    public List<Terminal> findAll() {
        List<Terminal> listData = terminalDao.findAll();
        return listData;
    }

    public List<Terminal> findById(int id) {
        List<Terminal> listData = terminalDao.findById();
        return listData;
    }

    public List<String> getDataByDatabaseId(Integer id) {
        return terminalDao.getDataByDatabaseId(id);
    }


}

and last this is my controller for rest api also will be add in swagger :

package com.api.elastic.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
//import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.api.elastic.entity.Terminal;
import com.api.elastic.model.RestResponseModel;
import com.api.elastic.service.PkService;
import com.api.elastic.service.TerminalService;
import com.api.elastic.utils.MessageDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@CrossOrigin(origins = "*")
@RestController
@RequestMapping(value = "terminal")
@SuppressWarnings({ "rawtypes", "unchecked" })
@Api(value = "Terminal API", description = "Operations pertaining to Terminal", tags = "Terminal")
public class TerminalController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    TerminalService terminalService;

    @ApiOperation(value = "Finde All Terminal", response = RestResponseModel.class)
    @GetMapping("/list")
    public ResponseEntity<RestResponseModel<Terminal>> findTerminalIndex() {

        List<Terminal> list = terminalService.findAll();
        int totalRow = 0;
        if (null != list) totalRow = list.size();

        RestResponseModel<Terminal> resp = new RestResponseModel<Terminal>();
        resp.setContent(list);
        resp.setTotalRow(totalRow);
        logger.info("totalRow : " + totalRow);
        MessageDetail info = new MessageDetail();
        if (totalRow == 0) {
            logger.info("NO_CONTENT ....");
            info.setMessage(HttpStatus.NO_CONTENT.name());
            info.setStatus(HttpStatus.NO_CONTENT.value());
            resp.setInfo(info);
            return new ResponseEntity<RestResponseModel<Terminal>>(resp, HttpStatus.OK);
        } else {
            info.setMessage(HttpStatus.OK.name());
            info.setStatus(HttpStatus.OK.value());
            resp.setInfo(info);
            return new ResponseEntity<RestResponseModel<Terminal>>(resp, HttpStatus.OK);
        }
    }

    @ApiOperation(value = "Get Data By Database Id", response = RestResponseModel.class)
    @RequestMapping(value="/list/{id}", method= RequestMethod.GET)
    public ResponseEntity<RestResponseModel<Terminal>> getDataByDatabaseId(@PathVariable("id") Integer id) {
        RestResponseModel<Terminal> resp = new RestResponseModel<Terminal>();
        List<Terminal> list = terminalService.findById(id);
        int totalRow = 0;
        if (null != list) totalRow = list.size();
        resp.setContent(list);
        resp.setTotalRow(totalRow);
        logger.info("totalRow : " + totalRow);
        MessageDetail info = new MessageDetail();   
        return new ResponseEntity<RestResponseModel<Terminal>>(resp, HttpStatus.OK);
    }

}

when I run I got error like this :

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'terminalService': Unsatisfied dependency expressed through field 'terminalDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'id.co.bca.api.elastic.repository.TerminalDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Honestly I tried google, but there is no pom.xml in this project.

So how to solve this?

Upvotes: 0

Views: 63

Answers (2)

Yash Jain
Yash Jain

Reputation: 822

In your Dao, you need to extend one of the repository. Either CrudRepository or JPARepository.

public interface TerminalDao extends CrudRepository<Terminal,Integer> {
    public boolean isExist(String p_index);
    public List<Terminal> findById();
    public List<Terminal> findAll();
    public List<String> getDataByDatabaseId(Integer id);

}

Upvotes: 0

Luiz E.
Luiz E.

Reputation: 7279

You defined an interface called TerminalDAO but there is no implementation of this class.

You should have something like this

@Component
class TerminalDaoImpl implements TerminalDao {
 public ....
}

keep in mind that if you are trying to use Spring Data, you should do this other way, just follow the documentation on how to define a repository

Upvotes: 3

Related Questions