考虑在您的配置中定义一个“包"类型的 bean [Spring-Boot] [英] Consider defining a bean of type 'package' in your configuration [Spring-Boot]
问题描述
我收到以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setApplicant in webService.controller.RequestController required a bean of type 'com.service.applicant.Applicant' that could not be found.
Action:
Consider defining a bean of type 'com.service.applicant.Applicant' in your configuration.
我以前从未见过此错误,但奇怪的是 @Autowire 不起作用.这是项目结构:
I have never seen this error before but it's odd that the @Autowire is not working. Here is the project structure:
申请人界面
public interface Applicant {
TApplicant findBySSN(String ssn) throws ServletException;
void deleteByssn(String ssn) throws ServletException;
void createApplicant(TApplicant tApplicant) throws ServletException;
void updateApplicant(TApplicant tApplicant) throws ServletException;
List<TApplicant> getAllApplicants() throws ServletException;
}
ApplicantImpl
ApplicantImpl
@Service
@Transactional
public class ApplicantImpl implements Applicant {
private static Log log = LogFactory.getLog(ApplicantImpl.class);
private TApplicantRepository applicantRepo;
@Override
public List<TApplicant> getAllApplicants() throws ServletException {
List<TApplicant> applicantList = applicantRepo.findAll();
return applicantList;
}
}
现在我应该能够只自动装配申请人并能够访问,但是在这种情况下,当我在我的 @RestController:
Now I should be able to just Autowire Applicant and be able to access, however in this case it is not working when I call it in my @RestController:
@RestController
public class RequestController extends LoggingAware {
private Applicant applicant;
@Autowired
public void setApplicant(Applicant applicant){
this.applicant = applicant;
}
@RequestMapping(value="/", method = RequestMethod.GET)
public String helloWorld() {
try {
List<TApplicant> applicantList = applicant.getAllApplicants();
for (TApplicant tApplicant : applicantList){
System.out.println("Name: "+tApplicant.getIndivName()+" SSN "+tApplicant.getIndSsn());
}
return "home";
}
catch (ServletException e) {
e.printStackTrace();
}
return "error";
}
}
------------------------更新 1--------------
------------------------UPDATE 1-----------------------
我加了
@SpringBootApplication
@ComponentScan("module-service")
public class WebServiceApplication extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(WebServiceApplication.class, args);
}
}
错误消失了,但什么也没发生.但是,当我在添加 @ComponentScan()
之前在 RestController
中注释掉处理 Applicant
的所有内容时,我能够返回一个字符串 >UI
,这意味着我的 RestController
正在工作,现在它被跳过了.我现在很丑Whitelabel Error Page
.
and the error went away but nothing happened. However when I commented out everything dealing with Applicant
in the RestController
prior to adding @ComponentScan()
I was able to return a string the UI
, thus meaning my RestController
was working, now it is being skipped. I ugly Whitelabel Error Page
now.
---------------------更新 2------------------------------
---------------------UPDATE 2------------------------------
我添加了它抱怨的 bean 的基础包.错误内容:
I added the base package of the bean it was complaining about. Error reads:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method setApplicantRepo in com.service.applicant.ApplicantImpl required a bean of type 'com.delivery.service.request.repository.TApplicantRepository' that could not be found.
Action:
Consider defining a bean of type 'com.delivery.request.request.repository.TApplicantRepository' in your configuration.
我添加了@ComponentScan
@SpringBootApplication
@ComponentScan({"com.delivery.service","com.delivery.request"})
public class WebServiceApplication extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(WebServiceApplication.class, args);
}
}
----------------------------更新3----------------------
----------------------------Update 3----------------------
添加:
@SpringBootApplication
@ComponentScan("com")
public class WebServiceApplication extends SpringBootServletInitializer {
仍然抱怨我的 ApplicantImpl
类将 @Autowires
我的 repo TApplicantRepository
放入其中.
still is complaining about my ApplicantImpl
class which @Autowires
my repo TApplicantRepository
into it.
推荐答案
我在使用 Spring Boot 2 的 Maven 多模块项目中遇到了熟悉的问题.该问题与子 Maven 模块中的包的命名有关.
I faced with familiar problem in my Maven multi-module project with Spring Boot 2. The problem was related to naming of my packages in sub Maven modules.
@SpringBootApplication 封装了很多组件,如@ComponentScan、@EnableAutoConfiguration、jpa-repositories、json-serialization 等.他将@ComponentScan 放在 com.*******.space 包中.这部分包 com.********.space 必须是所有模块通用的.
@SpringBootApplication incapsulate a lots of component like - @ComponentScan, @EnableAutoConfiguration, jpa-repositories, json-serialization and so on. And he places @ComponentScan in com.*******.space package. This part of packages com.*******.space must be common for all modules.
修复:
- 您应该重命名所有模块包.换句话说,您必须在所有 Maven 模块的所有包中拥有相同的父部分.例如 - com.*******.space
- 此外,您还必须将入口点移至此包 - com.*******.space
这篇关于考虑在您的配置中定义一个“包"类型的 bean [Spring-Boot]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!