当前位置: 首页 > 知识库问答 >
问题:

UnsatisfiedDependencyException:创建具有名称的bean时出错

越福
2023-03-14

还有这个

UnsatisfiedDependencyException:创建名为“Client Service”的bean时出错:通过字段“Client Repository”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有“com.kopylov.repository.clientrepository”类型的合格bean可用:应至少有一个合格的自动候选bean。依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(required=true)}

客户端控制

@Controller
public class ClientController {
private ClientService clientService;

@Autowired
@Qualifier("clientService")
public void setClientService(ClientService clientService){
    this.clientService=clientService;
}
@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client){
    this.clientService.addClient(client);
return "home";
}
}
@Service("clientService")
public class ClientServiceImpl implements ClientService{

private ClientRepository clientRepository;

@Autowired
@Qualifier("clientRepository")
public void setClientRepository(ClientRepository clientRepository){
    this.clientRepository=clientRepository;
}



@Transactional
public void addClient(Client client){
    clientRepository.saveAndFlush(client);
}
}
public interface ClientRepository extends JpaRepository<Client, Integer> {

}

共有1个答案

苗烈
2023-03-14

应使用@repository标记对ClientRepository进行注释。使用您当前的配置,Spring将不会扫描类并了解它。在引导和连接时,将找不到ClientRepository类。

编辑如果添加@repository标记没有帮助,那么我认为问题可能出在ClientServiceClientServiceImpl上。

尝试用@service注释clientservice(接口)。由于您的服务应该只有一个实现,因此不需要使用可选参数@service(“clientservice”)指定名称。Spring将根据接口的名称自动生成它。

而且,正如Bruno提到的,ClientController中不需要@qualifier,因为您只有一个服务实现。

ClientService.java

@Service
public interface ClientService {

    void addClient(Client client);
}

ClientServiceImpl.java(选项1)

@Service
public class ClientServiceImpl implements ClientService{

    private ClientRepository clientRepository;

    @Autowired
    public void setClientRepository(ClientRepository clientRepository){
        this.clientRepository=clientRepository;
    }

    @Transactional
    public void addClient(Client client){
        clientRepository.saveAndFlush(client);
    }
}
@Service
public class ClientServiceImpl implements ClientService{

    @Autowired
    private ClientRepository clientRepository;

    @Transactional
    public void addClient(Client client){
        clientRepository.saveAndFlush(client);
    }
}
@Controller
public class ClientController {
    private ClientService clientService;

    @Autowired
    //@Qualifier("clientService")
    public void setClientService(ClientService clientService){
        this.clientService=clientService;
    }

    @RequestMapping(value = "registration", method = RequestMethod.GET)
    public String reg(Model model){
        model.addAttribute("client", new Client());
        return "registration";
    }

    @RequestMapping(value = "registration/add", method = RequestMethod.POST)
    public String addUser(@ModelAttribute Client client){
        this.clientService.addClient(client);
    return "home";
    }
}
 类似资料: