Spring 弹簧罐';找不到自动连线接口实现
我在这里有一个主要的SpringBootApplication类:Spring 弹簧罐';找不到自动连线接口实现,spring,spring-boot,dependency-injection,spring-ioc,Spring,Spring Boot,Dependency Injection,Spring Ioc,我在这里有一个主要的SpringBootApplication类: package com.example.springproj; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } package com.example.springproj.control
package com.example.springproj;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
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
}
}
@RestController类在此处:
package com.example.springproj;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
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.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.
我有理由相信这种自动连接应该可以工作,但我不确定如何在SpringBoot应用程序中配置这个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);
}
}
更改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);
}
}
作为参考,这些文件属于应用程序的包结构,如下所示:
com.example.springproj
-> com.example.springproj.controller
--> RefDataController
-> com.example.springproj.services
--> RefDataService
-> com.exampple.springproj.services.impl
---> RefDataServiceImpl
以下是文件夹结构,因为有人问:
更改
refdataservicecimpl
类上的@Service
注释,如下所示:
@Service("RefDataServiceImpl")
public class RefDataServiceImpl implements RefDataService
autowired服务中的@限定符
名称与spring配置中的bean不匹配。
默认命名约定是类的完整路径。
因此,,
Spring可能在您的配置中为RefDataServiceImpl
服务使用的名称是:“com.example.springproj.services.RefDataServiceImpl”
增加:
此页面可能是一个很好的阅读:
尝试二:
试试这个
@服务
@限定符(“RefDataServiceImpl”)
@服务(“RefDataServiceImpl”)
公共类RefDataServiceImpl实现RefDataService
首先,如果只有一个RefDataService
接口的实现,则不需要@Qualifier(“refdataserviceinpl”)
。
你只需要
@Autowired
private RefDataService refDataService;
其次,bean的名称是在类名上生成的,但以小写字母开头。在您的示例中,bean的名称看起来像refdataservicecimpl
。
因此,您可以像下面这样自动连接这个bean
@Autowired
@Qualifier("refDataServiceImpl")
private RefDataService refDataService;
第三,您可以指定bean的名称
@Service("youBeanName")
public class RefDataServiceImpl implements RefDataService
然后根据控制器中的名称自动连接这个bean,例如
@RestController
@Api("Sample")
public class RefDataController {
@Autowired
@Qualifier("youBeanName")
private RefDataService refDataService;
//....
}
我通过将
refdataserviceinpl
与RefDataService
放在同一个包中解决了这个问题。在此之前,我将它保存在主服务
包的子文件夹中。我仍然确信我应该能够通过实现子文件夹来实现这一点,但现在这是一个解决方案。我在尝试使用数据库查询实现类时遇到了同样的问题。将@Repository
添加到已实现类的顶部解决了我的问题。尝试了以下操作:似乎不起作用。我遇到了相同的错误。我认为问题在于在限定符中命名refdataserviceinpl
将其更改为refdataserviceinpl
您可以共享您的文件夹结构吗?你的应用程序类别在哪里?在帖子中添加了文件夹结构。请参阅上面帖子的评论
@RestController
@Api("Sample")
public class RefDataController {
@Autowired
@Qualifier("youBeanName")
private RefDataService refDataService;
//....
}