Spring找不到Autowired接口实现

Kri*_*ell 3 spring dependency-injection spring-ioc spring-boot

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

package com.example.springproj;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

@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
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器自动连接此接口:

package com.example.springproj.service;

public interface RefDataService {

    Configuration getConfiguration(String param);
}
Run Code Online (Sandbox Code Playgroud)

这是由这个类实现的:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行 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.
Run Code Online (Sandbox Code Playgroud)

我相当确定这个自动装配应该可以工作,并且我不确定如何在 Spring boot 应用程序中配置这个 bean。我究竟做错了什么?

编辑:我已经尝试过的事情包括:

删除所有 @Qualifier 注释

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

    @Autowired
    private RefDataServiceImpl refDataService;

    @GetMapping(path = {"/refdata"}, produces = {"application/json"})
    public ResponseEntity<Configuration> getRefData() {
        System.err.println("testing.");
        return new ResponseEntity<Configuration>(refDataService.getConfiguration("EEMS_USER_DETAIL_URL"), HttpStatus.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)
public class RefDataServiceImpl implements RefDataService {

    @Autowired
    private ConfigRepository config;

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

    @Override
    public Configuration getConfiguration(String param) {
        try {
            return config.getConfiguration(param, environment);
        } catch (Exception e) {
            e.printStackTrace();
            throw (RuntimeException) new RuntimeException().initCause(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更改 bean 名称以符合约定

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

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

    @GetMapping(path = {"/refdata"}, produces = {"application/json"})
    public ResponseEntity<Configuration> getRefData() {
        System.err.println("testing.");
        return new ResponseEntity<Configuration>(refDataService.getConfiguration("EEMS_USER_DETAIL_URL"), HttpStatus.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)
@Service("refDataServiceImpl")
public class RefDataServiceImpl implements RefDataService {

    @Autowired
    private ConfigRepository config;

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

    @Override
    public Configuration getConfiguration(String param) {
        try {
            return config.getConfiguration(param, environment);
        } catch (Exception e) {
            e.printStackTrace();
            throw (RuntimeException) new RuntimeException().initCause(e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

作为参考,这些文件属于应用程序的包结构,如下所示:

com.example.springproj
-> com.example.springproj.controller
--> RefDataController
-> com.example.springproj.services
--> RefDataService
-> com.exampple.springproj.services.impl
---> RefDataServiceImpl
Run Code Online (Sandbox Code Playgroud)

这是文件夹结构,因为有些人问:

在此输入图像描述

Myk*_*ura 5

@Qualifier("RefDataServiceImpl")首先,如果您只有一个接口实现,则不需要RefDataService。您只需

@Autowired
private RefDataService refDataService;
Run Code Online (Sandbox Code Playgroud)

其次,根据类名生成但以小写字母开头的 bean 名称。在您的示例中,bean 的名称将类似于refDataServiceImpl。所以,你可以像下面这样自动装配这个 bean

@Autowired
@Qualifier("refDataServiceImpl")
private RefDataService refDataService;
Run Code Online (Sandbox Code Playgroud)

第三,可以指​​定bean的名称

@Service("youBeanName")
public class RefDataServiceImpl implements RefDataService
Run Code Online (Sandbox Code Playgroud)

然后通过控制器中的名称自动装配该 bean,例如

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

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

    //....
}
Run Code Online (Sandbox Code Playgroud)