我得到以下错误:

***************************
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;
    }
}

现在我应该能够自动连线申请人,并能够访问,但在这种情况下,当我在@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中的申请人相关的内容时,我能够返回一个字符串的UI,从而意味着我的RestController正在工作,现在它被跳过了。我丑陋的白色标签错误页现在。

--------------------- 更新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类@自动连接我的repo TApplicantRepository到它。


当前回答

如果@Service类被标记为抽象,就会发生这种情况。

其他回答

我认为,您在RequestController中遗漏了@Bean注释

在文件中添加Bean,这解决了我的问题 当我从tutorialspoint学习Spring Boot时,我得到了这个解决方案

private Applicant applicant;

@Bean 
public Applicant applicant() { 
    return new Applicant(); 
}

如果您正在使用Eclipse并尝试了所有可能的方法,但仍然存在错误,那么请尝试使用Maven更新项目。您可以这样做:右键单击您的项目-> Maven ->更新项目。

它帮助我解决了我的错误。如果项目没有更新pom文件中声明的所有repo, Eclipse有时会显示此错误。

添加Spring Boot Data JPA Starter依赖为我解决了这个问题。

Maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

Gradle

compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.6.RELEASE'

或者你可以直接到这里

如果bean位于@Autowired所在的同一个包中,那么它将永远不会引起这样的问题。但是,默认情况下,bean不能从不同的包中访问。 要修复此问题,请执行以下步骤:

在你的主类中导入以下内容: 进口org.springframework.context.annotation.ComponentScan; 在主类上添加注释:

@ComponentScan(basePackages = {"your.company.domain.package"})
public class SpringExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringExampleApplication.class, args);
    }
}

重要的是:

对于那些通过谷歌通用bean错误消息而来到这里,但实际上试图通过客户端接口上的@FeignClient注释将一个虚拟客户端添加到Spring Boot应用程序的人来说,上述解决方案都不适合您。

为了解决这个问题,你需要在你的Application类中添加@EnableFeignClients注释,如下所示:

@SpringBootApplication
// ... (other pre-existing annotations) ...
@EnableFeignClients // <------- THE IMPORTANT ONE
public class Application {

旁注:在@SpringBootApplication下面添加@ComponentScan(…)是多余的,你的IDE应该这样标记它(至少IntelliJ IDEA是这样做的)。