Spring找不到自动接线的接口实现

问题描述 投票:0回答:1

我在这里有一个主要的SpringBootApplication类:

package com.example.springproj;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

(我得到

这里是@RestController类:

package com.example.springproj.controller;

@RestController
@Api("Sample")
public class RefDataController {

    @Autowired
    @Qualifier("RefDataServiceImpl")
    private RefDataService refDataService;

    @GetMapping(path = {"/refdata"}, produces = {"application/json"})
    public ResponseEntity<Configuration> getRefData() {
        // etc
    }
}

控制器自动连接此接口:

package com.example.springproj.service;

public interface RefDataService {

    Configuration getConfiguration(String param);
}

哪个由此类实现:

package com.example.springproj.services;
@Service
public class RefDataServiceImpl implements RefDataService {

    @Autowired
    private ConfigRepository config;

    @Value("${ENV}")
    private String environment;

    @Override
    public Configuration getConfiguration(String param) {
        // etc
    }
}

但是当我运行App.java文件时,我得到了这个

***************************
APPLICATION FAILED TO START
***************************

Description:

Field refDataService in com.citi.icrm.risk.springproj.controller.RefDataController required a bean of type 'com.citi.icrm.risk.springproj.service.RefDataService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
    - @org.springframework.beans.factory.annotation.Qualifier(value=RefDataServiceImpl)


Action:

Consider defining a bean of type 'com.citi.icrm.risk.springproj.service.RefDataService' in your configuration.

我有合理的把握,这种自动装配应该可以工作,而且我不确定如何在Spring Boot应用程序中配置此bean。我在做什么错?

spring spring-boot
1个回答
0
投票

如下更改@Service类上的RefDataServiceImpl注释:

@Service("RefDataServiceImpl")
public class RefDataServiceImpl implements RefDataService

autowired服务中的@Qualifier名称与您的spring配置中的bean不匹配。默认的命名约定是该类的完整路径。因为这,Spring可能在您的配置中为RefDataServiceImpl服务使用的名称是:“ com.example.springproj.services.RefDataServiceImpl”。

© www.soinside.com 2019 - 2024. All rights reserved.