gpt4 book ai didi

Spring找不到Autowired接口(interface)实现

转载 作者:行者123 更新时间:2023-12-05 01:38:51 24 4
gpt4 key购买 nike

我这里有一个主要的 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
}
}

Controller 自动连接这个接口(interface):

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.

我有理由相信这个 Autowiring 应该可以工作,但我不确定如何在 Spring 引导应用程序中配置这个 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);
}
}
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);
}
}
}

更改 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);
}
}
@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);
}
}
}

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

com.example.springproj
-> com.example.springproj.controller
--> RefDataController
-> com.example.springproj.services
--> RefDataService
-> com.exampple.springproj.services.impl
---> RefDataServiceImpl

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

enter image description here

最佳答案

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

@Autowired
private RefDataService refDataService;

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

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

第三,你可以指定bean的名字

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

然后通过 Controller 中的名称 Autowiring 这个 bean,例如

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

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

//....
}

关于Spring找不到Autowired接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59273911/

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