我得到以下错误:

***************************
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到它。


当前回答

如果您意外地在两个不同的类中定义了相同的bean,也会得到这个错误。我就是这样。错误信息具有误导性。当我去掉多余的豆子时,问题就解决了。

其他回答

你的申请人类似乎没有被扫描。默认情况下,所有以根目录开头的包都将被扫描,你在其中放置了@SpringBootApplication类。

假设你的主类“WebServiceApplication”在“com.service”中。Something”,然后是属于“com.service”的所有组件。“某些东西”被扫描,而“com.service.”“申请人”将不会被扫描。

你可以重新构造你的包,使“WebServiceApplication”置于根包之下,而所有其他组件都成为根包的一部分。或者你可以包含@SpringBootApplication(scanBasePackages={"com.service.something","com.service.application"})等,这样"ALL"组件就会在spring容器中被扫描和初始化。

根据评论进行更新

如果你有多个由maven/gradle管理的模块,spring所需要的就是要扫描的包。你告诉春天扫描“com”。Module1”,还有另一个模块,其根包名为“com”。Module2”,这些组件不会被扫描。您甚至可以告诉spring扫描“com”,然后它将扫描“com.module1.”和“com.module2.”中的所有组件。

修复如下错误:

考虑在您的配置中定义一个'package'类型的bean 不是托管类型

我碰巧发现LocalContainerEntityManagerFactoryBean类没有setpackagestscan方法。然后我继续使用@ entitscan注释,它不能正常工作。

后来,我可以找到方法setPackagesToScan(),但在另一个模块,所以问题来自于依赖没有这个方法,因为它是一个旧版本。

这个方法可以在更新版本的spring-data-jpa或spring-orm依赖项中找到:

来自:

implementation("org.springframework", "spring-orm", "2.5.1") 

To:

implementation("org.springframework", "spring-orm", "5.2.9.RELEASE")

或:

implementation("org.springframework.data", "spring-data-jpa", "2.3.4.RELEASE")

此外,没有必要添加除的注释之外的其他注释 @SprintBootApplication。

@SpringBootApplication
open class MoebiusApplication : SpringBootServletInitializer()

@Bean
open fun entityManagerFactory() : LocalContainerEntityManagerFactoryBean {
    val em = LocalContainerEntityManagerFactoryBean()
    em.dataSource = dataSource()
    em.setPackagesToScan("app.mobius.domain.entity")
    ... 
}

GL

对我来说,在pom.xml上进行了干净的安装

右键单击pom.xml 扩展运行为 选择Maven构建 将目标设置为clean install命令 应用>运行>关闭

我通过将此配置添加到Application.java文件来修复它。

重要的是:

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

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

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

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