VanRoy / spring-data-jest

Spring Data Implementation for Jest
Apache License 2.0
227 stars 78 forks source link

JestElasticsearchTemplate not able to do AutoWired. #148

Open sats17 opened 4 years ago

sats17 commented 4 years ago

Hi @VanRoy , I tried to autowire the JestElasticsearchTemplate and it is returning below error.

Action: Consider defining a bean of type 'com.github.vanroy.springdata.jest.JestElasticsearchTemplate' in your configuration.

for code

@Service public class ElasticSearchService { @Autowired private JestElasticsearchTemplate template; }

and dependency I am using is

com.github.vanroy spring-boot-starter-data-jest 3.3.0.RELEASE

After checking all beans that is created in my application I did not found any bean for JestElasticsearchTemplate. But I see bean for elasticsearchTemplate is created.

Also when I am not getting any exception when I am autowiring an interface @Autowired private ElasticsearchOperations template;

and after the getting class of above template class variable I can see it is pointing to JestElasticsearchTemplate class, but again after this I checked all beans which is created in Container I did not found JestElasticsearchTemplate bean is created.

It all looks strange to me as 1) Error is throwing for JestElasticsearchTemplate while performing autowiring on that. 2) How in my container I did not found any bean name as JestElasticsearchTemplate. 3) And even if there is not any bean created in my container how @Autowired private ElasticsearchOperations template is pointing to JestElasticsearchTemplate class.

VanRoy commented 4 years ago

Hi @sats17 , are you sure that you have exclude ElasticSearchAutoConfiguration like this :

@SpringBootApplication(exclude = {ElasticsearchAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class})

And that at least this properties is set :

spring:
    data:
        jest:
            uri: http://localhost:9200
sats17 commented 4 years ago

Hi @VanRoy,

I did all what you mentioned, It's my bad that I was just searching beans with bean names. But later that I found bean for JestElasticSearchTemplate which is

"elasticsearchTemplate": { "aliases": [], "scope": "singleton", "type": "com.github.vanroy.springdata.jest.JestElasticsearchTemplate", "resource": "class path resource [com/github/vanroy/springboot/autoconfigure/data/jest/ElasticsearchJestDataAutoConfiguration.class]", "dependencies": [ "testClient" ] },

As of now I can see that if I did like below, my project compiles without any error.

@Autowired private ElasticsearchOperations esTemplate;
@Autowired private JestElasticsearchTemplate jestTemplate;

But if I swapped the positions or remove the ElasticsearchOperations, then spring boot not able to found bean for JestElasticsearchTemplate.

@Autowired private JestElasticsearchTemplate jestTemplate; @Autowired private ElasticsearchOperations esTemplate;

VanRoy commented 4 years ago

Hi @sats17, Indeed it's really strange 😄 . I will investigate on it.

sats17 commented 3 years ago

Hi @VanRoy , I have attached the zip file of the test project which is having this bean problem.

test.zip

VanRoy commented 3 years ago

Hi @sats17 , thanks a lot for your additionnals informations. Indeed, currently SpringDataJest expose his JestElasticsearchTemplate bean as ElasticsearchOperations type. For now your best option to use JestElasticsearchTemplate is to do something like that 👍

public class MyClass {

private final JestElasticsearchTemplate jestTemplate;

public MyClass(ElasticsearchOperations esTemplate) {
   this.jestTemplate = (JestElasticsearchTemplate)esTemplate;
}

I a futur release I will probably change the type of the JestElasticsearchTemplate bean from ElasticsearchOperations to JestElasticsearchTemplate but I need to check the possible impacts.

Again, thanks for your help during the investigation. Julien.

sats17 commented 3 years ago

Hi @VanRoy ,

Thanks for the clarification :smiley: