spring-data-jest icon indicating copy to clipboard operation
spring-data-jest copied to clipboard

JestElasticsearchTemplate not able to do AutoWired.

Open sats17 opened this issue 4 years ago • 6 comments

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 <groupId>com.github.vanroy</groupId> <artifactId>spring-boot-starter-data-jest</artifactId> 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.

sats17 avatar Oct 21 '20 04:10 sats17

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

VanRoy avatar Oct 22 '20 17:10 VanRoy

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;

sats17 avatar Oct 24 '20 14:10 sats17

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

VanRoy avatar Nov 01 '20 19:11 VanRoy

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

test.zip

sats17 avatar Nov 28 '20 11:11 sats17

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.

VanRoy avatar Dec 04 '20 20:12 VanRoy

Hi @VanRoy ,

Thanks for the clarification :smiley:

sats17 avatar Dec 05 '20 07:12 sats17