我得到以下错误:

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


当前回答

我遇到了类似的问题,完全相同的错误信息。 但我的错误只发生在我试图安装和运行在我的本地docker。

事实证明,这个问题取决于个人特征。

我有两个实现接口的类,其中一个具有生产Profile @Profile(“prod”) 第二个是“测试环境”概要文件。 @Profile(”!本地& !prod”)

然而,当我试图在本地(在Docker上)运行时,没有活动配置文件。

我创建了一个第三类,实现了一个本地配置文件@Profile(“本地”)的接口,这解决了我的问题

其他回答

如果您正在使用Lombok,并且为字段添加了@RequiredArgsConstructor和@NonNull,但有些字段不会被注入到构造函数中,也会发生这种情况。这只是得到相同错误的一种可能。

参数0需要一个MissingBeanName类型的bean,但是找不到

在我的情况下,错误告诉我什么控制器的问题,删除@NonNull后,应用程序启动良好

我也收到了类似的错误:

Consider defining a bean of type 'A_REPOSITORY_INTERFACE' in your configuration.

然后,根据Akashe的解决方案,我将@ enablejparepository添加到我的主类中。在那之后,我收到了以下错误:

Consider defining a bean of type 'entityManagerFactory' in your configuration.

接下来,我浏览了这里所有的回复,谷歌了很多,阅读了很多其他资源,但没有奏效。

最后,我很幸运地在博客/网站(javatute.com)上找到了解决方案。我只是效仿它。

就像许多人在这里建议的那样,我添加了@ComponentScan(“YOUR_BASE_PACKAGE.*”)和@ entitscan(“YOUR_BASE_PACKAGE.*”)到我的主应用程序类中,然后添加了一个配置包并创建了一个JpaConfig类,如:

package YOUR_BASE_PACKAGE.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EnableJpaRepositories(basePackages = "YOUR_BASE_PACKAGE")
public class JpaConfig {

}

我关注的博客:

考虑在您的配置中定义一个bean类型

这让我想到:

在类路径资源中创建名称为entityManagerFactory的bean时出错:init方法调用失败

最后是:

Hibernate/JPA中使用Spring Boot和Oracle的多对多映射

在应用程序中添加以下注释后,它为我工作:

@ComponentScan ({com.seic.deliveryautomation.mapper "})

我得到以下错误:

"构造函数中的参数1需要一个类型映射器的bean,但找不到:

当你使用@EnableMongoRepositories(YOUR_MONGO_REPOSITORIES_PACKAGE),然后重命名包名或将其移动到另一个地方时,问题也会出现。

在多模块maven项目和spring引导中经常遇到这种情况

我的错误在于,我把以下内容包括进去了:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

而不是:

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