gpt4 book ai didi

java - 当我添加新的存储库和服务时,Spring 框架 bean 工厂不满意

转载 作者:行者123 更新时间:2023-12-01 19:50:06 25 4
gpt4 key购买 nike

我对 Spring 框架还是新手。我的客户使用spring框架+boot+swagger。

我尝试添加新的 Dao 作为存储库,如下所示:

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

}

然后我添加服务:

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


}

最后,这是我的其余 api Controller 也将添加到 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);
}

}

当我运行时出现这样的错误:

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

老实说,我尝试了谷歌,但这个项目中没有pom.xml

那么如何解决这个问题呢?

最佳答案

您定义了一个名为 TerminalDAO 的接口(interface),但没有此类的实现。

你应该有这样的东西

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

请记住,如果您尝试使用 Spring Data,则应该以其他方式执行此操作,只需按照 how to define a repository 上的文档进行操作即可。

关于java - 当我添加新的存储库和服务时,Spring 框架 bean 工厂不满意,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51611469/

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