我得到以下错误:

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


当前回答

在我的例子中,我们的项目有一个Configuration类,所以我像这样添加了我的类

@Configuration
public class DependencyConfiguration {

    @Bean
    public ActivityService activityService(
            @Value("${send.money.ms.activity.url}") final String activityHistoryUrl,
            final HttpRestService httpRestService
    ) {
        return new ActivityServiceImpl(activityHistoryUrl, httpRestService);
    }

.......................

然后微服务就正常启动了。

PS:我遇到了这个问题,即使我需要的库已经正确导入,并且可以在导入的外部库中看到。

其他回答

我在Spring Boot 2的Maven多模块项目中遇到了熟悉的问题。这个问题与子Maven模块中包的命名有关。

@SpringBootApplication封装了很多组件,比如@ComponentScan, @EnableAutoConfiguration, jpa-repositories, json-serialization等等。他把@ComponentScan放在了com.*******中。空间方案。这部分的包com.*******。所有模块的空间必须是公用的。

解决方法:

您应该重命名所有模块包。换句话说,您必须在所有Maven模块的所有包中都有相同的父部分。例如- com.*******.space 此外,您必须将您的入口点移动到这个包- com.*******.space

如果你使用接口,你可以扩展CrudRepository<申请人,长> @Repository注释。

如果您有两个名称相同的bean,也会发生这种情况。

例如,如果你有一个工厂方法monkey创建了monkey,而你错误地将另一个工厂方法(创建Car)也命名为monkey。

有问题的代码:

    @Bean
    public Car monkey() {
        return new Car();
    }

应该是:

    @Bean
    public Car car() {
        return new Car();
    }

参考:

默认情况下,bean名称将与方法名称相同…

春季文档1

通过属性名自动装配。春天找一颗同样的豆子 名称作为需要自动连接的属性。例如,如果a Bean定义被设置为按名称自动装配,并且它包含一个master 属性(也就是说,它有一个setMaster(..)方法),Spring寻找一个 Bean定义名为master,并使用它来设置属性。

春季文档2

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

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

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

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

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

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

<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>