java - Autowired KafkaTemplate in SpringBoot/spring-kafka application throws null pointer -


i'm trying use spring-kafka kafkatemplate, inside spring boot application, write messages kafka topic.

i created kafkaconfig class:

@configuration @enablekafka public class kafkaconfig {      @value("${kafka.broker.address}")     private string brokeraddress;      @bean     public producerfactory<integer, string> producerfactory() {         return new defaultkafkaproducerfactory<>(producerconfigs());     }      @bean     public map<string, object> producerconfigs() {         map<string, object> props = new hashmap<>();         props.put(producerconfig.bootstrap_servers_config, brokeraddress);         props.put(producerconfig.retries_config, 0);         props.put(producerconfig.batch_size_config, 16384);         props.put(producerconfig.linger_ms_config, 1);         props.put(producerconfig.buffer_memory_config, 33554432);         props.put(producerconfig.key_serializer_class_config, integerserializer.class);         props.put(producerconfig.value_serializer_class_config, stringserializer.class);          return props;     }      @bean     public kafkatemplate<integer, string> kafkatemplate() {         return new kafkatemplate<integer, string>(producerfactory());     }  } 

... , autowired kafkatemplate in class i'm writing kafka:

@autowired private kafkatemplate<integer, string> template; 

for reason, autowiring doesn't appear work. notice kafkatemplate null when run in debug:

template null

this object should not null; should kafkatemplate object. throws null pointer exception:

org.springframework.beans.factory.beancreationexception: error creating bean name 'watchservice': invocation of init method failed; nested exception java.lang.nullpointerexception     @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor.postprocessbeforeinitialization(initdestroyannotationbeanpostprocessor.java:136) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applybeanpostprocessorsbeforeinitialization(abstractautowirecapablebeanfactory.java:408) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1570) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:545) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:776) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:861) ~[spring-context-4.3.2.release.jar:4.3.2.release]     @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:541) ~[spring-context-4.3.2.release.jar:4.3.2.release]     @ org.springframework.boot.springapplication.refresh(springapplication.java:759) [spring-boot-1.4.0.release.jar:1.4.0.release]     @ org.springframework.boot.springapplication.refreshcontext(springapplication.java:369) [spring-boot-1.4.0.release.jar:1.4.0.release]     @ org.springframework.boot.springapplication.run(springapplication.java:313) [spring-boot-1.4.0.release.jar:1.4.0.release]     @ org.springframework.boot.springapplication.run(springapplication.java:1185) [spring-boot-1.4.0.release.jar:1.4.0.release]     @ org.springframework.boot.springapplication.run(springapplication.java:1174) [spring-boot-1.4.0.release.jar:1.4.0.release]     @ io.woolford.application.main(application.java:11) [classes/:na] caused by: java.lang.nullpointerexception: null     @ io.woolford.watchservice.monitor(watchservice.java:93) ~[classes/:na]     @ io.woolford.watchservice.watchpath(watchservice.java:37) ~[classes/:na]     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[na:1.8.0_91]     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) ~[na:1.8.0_91]     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[na:1.8.0_91]     @ java.lang.reflect.method.invoke(method.java:498) ~[na:1.8.0_91]     @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor$lifecycleelement.invoke(initdestroyannotationbeanpostprocessor.java:365) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor$lifecyclemetadata.invokeinitmethods(initdestroyannotationbeanpostprocessor.java:310) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     @ org.springframework.beans.factory.annotation.initdestroyannotationbeanpostprocessor.postprocessbeforeinitialization(initdestroyannotationbeanpostprocessor.java:133) ~[spring-beans-4.3.2.release.jar:4.3.2.release]     ... 17 common frames omitted 

for sake of reproducibility, posted code on github.

kafka-spring has worked me in past using identical code, , i'm bit confused why autowiring isn't working time. can see i'm doing wrong?

you using bean soon...

postprocessbeforeinitialization 

...that's invoked before properties set.

edit

implement smartlifecycle , move

watchservice.monitor(path); 

to start().

edit2

that said; weird...

    watchservice watchservice = new watchservice();     watchservice.monitor(path); 

you creating new instance in post construct method - of course won't autowired because it's not managed spring.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -