Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider defining a bean of type 'com.xxx.ElasticsearchRestClient' in your configuration #19603

Closed
ouminAsk opened this issue Jan 10, 2020 · 2 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@ouminAsk
Copy link

Affects: Spring Framework version 5.1.10 & spring boot version 2.1.9

hello , my project use spring boot version 2.1.9 , Spring Framework version is 5.1.10
my class :

@Component
public class ElasticsearchRestClient {


    public  void test(){
        System.out.println("aaa");
    }

}

my use class:

@Component
public class OrganizationEsHelper {


   @Autowired
    private ElasticsearchRestClient client;

}    

then it has a error:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-10 10:46:08.665 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

 in com.newpearl.dc.service.es.OrganizationEsHelper required a bean of type 'com.newpearl.dc.service.es.ElasticsearchRestClient' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.newpearl.dc.service.es.ElasticsearchRestClient' in your configuration.

but rename Classname ElasticsearchRestClient to ElasticSearchRestClient. It runs ok .
I think it is a bug.
I don't have this problem in another project(spring boot version 1.5.9 , spring version 4.3.13 ) 。

@snicoll snicoll transferred this issue from spring-projects/spring-framework Jan 10, 2020
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 10, 2020
@snicoll
Copy link
Member

snicoll commented Jan 10, 2020

That ElasticsearchRestClient is actually auto-configured by Spring Boot with a different type (but the same bean name). Because they have the same name and no ElasticsearchRestClient is defined (the type from Elasticsearch, not yours), the auto-configuration applies and overrides your bean. When you try to inject your bean, it fails because it wasn't created in the first place

In Spring Boot 2.2. we prevent bean overriding by default so if you upgrade, you'll get a failure analysis that states that the auto-configuration can't override the bean you've defined. Hopefully, this is a much richer error detail that helps you figure out what went wrong.

This is a general problem with bean naming. You could consider renaming your type, provide a different name via the @Component annotation or customize how names for scanned components are determined. We've recently added a way to customize the BeanNameGenerator via @SpringBootApplication.

@snicoll snicoll closed this as completed Jan 10, 2020
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 10, 2020
@ouminAsk
Copy link
Author

Oh, I see. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants