【发布时间】:2017-03-16 00:11:33
【问题描述】:
我收到以下错误:
***************************
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 不起作用。这是项目结构:
申请人界面
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
@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;
}
}
现在我应该能够只使用 Autowire 申请人并能够访问,但是在这种情况下,当我在我的 @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--------- --
我加了
@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。
----------更新 2------------------------ ------
我添加了它抱怨的 bean 的基本包。错误读取:
***************************
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---------------- -----
添加:
@SpringBootApplication
@ComponentScan("com")
public class WebServiceApplication extends SpringBootServletInitializer {
仍然在抱怨我的ApplicantImpl 课程,其中@Autowires 我的回购TApplicantRepository 进入了它。
【问题讨论】:
-
您的应用程序上下文文件在哪里?如果你没有,你应该考虑给 Spring 一些提示,比如 @ComponentScan 以使所有 bean 都可用。
-
@MarioSantini 请参阅更新 1
-
我假设每次更新后错误都会发生变化?如果可能,请发布您的项目结构,以及每种情况下的错误日志/堆栈跟踪。最好知道“为什么”发生这些错误,而不是“某事”使错误消失。也会对遇到类似问题的其他人有所帮助。
标签: java spring-boot