Axonframework: Aggregate Repository Spring wiring causes NullPointerException

Created on 12 Jun 2019  路  14Comments  路  Source: AxonFramework/AxonFramework

Updated Description
Given an Axon Framework application using the axon-spring-boot-starter for auto wiring purposes, currently a NullPointerException is thrown if you try to wire an Aggregate Repository<A>.
This occurs because the parent Configuration of an AggregateConfigurer which is used to correctly instantiate an Aggregate's Repository<A>, is null.

Thus, the RepositoryFactoryBean is wired to the Application Context earlier then the initialization of the AggregateConfigurer, which is undesirable.

Marked as a priority 2 issue, as there is a work around for resolving this.

Original Message
if you check out this https://github.com/Sam-Kruglov/bike-rental-demo/tree/help
and try to run one of the apps, e.g. with profile "rental", then if you remove this part you get an NPE.
https://github.com/Sam-Kruglov/bike-rental-demo/blob/bdaa71b0f5fb8c35604c7e6bba9119dd1063bcad/src/main/kotlin/io/axoniq/demo/bikerental/BikeService.kt#L17

Should Bug

All 14 comments

Thanks for filing this issue @Sam-Kruglov, but I do not think this is the right spot to discuss this issue. The Bike Rental Demo is an application which @abuijze wrote for a presentation, so in my honest opinion should no form the direct basis for 'an issue' at the Axon Framework repository.

A readjusted description might proof it valuable to take into account, but for now I'd like to refer these kinds of discussions either towards Stack Overflow together with the tag axon or towards the Axon User Group.

@smcvb thanks for the reply! This is not Alard's repository, although the idea is the same.
Axon Framework throws NPE during bean initialization, why is it not an issue? The workaround is to wait for event gateway initialization. Seems like a framework bug to me

Hi @Sam-Kruglov, sorry for the late reply back. Times are busy as I've told you earlier.

To verify the statement in the description of this issue, I've just cloned your repository and checked out the help branch. Then, I started the application, with profile rental, and had Axon Server running in the background.

This is my console output:

/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -Dvisualvm.id=11635767877192 -XX:TieredStopAtLevel=1 -noverify -Dspring.profiles.active=rental -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:/opt/idea-IU-191.7141.44/lib/idea_rt.jar=43161:/opt/idea-IU-191.7141.44/bin -Dfile.encoding=UTF-8 -classpath /home/steven/workspace/test/bike-rental-demo/target/classes:/home/steven/.m2/repository/org/axonframework/axon-spring-boot-starter/4.1.1/axon-spring-boot-starter-4.1.1.jar:/home/steven/.m2/repository/org/axonframework/axon-spring-boot-autoconfigure/4.1.1/axon-spring-boot-autoconfigure-4.1.1.jar:/home/steven/.m2/repository/org/axonframework/axon-server-connector/4.1.1/axon-server-connector-4.1.1.jar:/home/steven/.m2/repository/io/grpc/grpc-protobuf/1.19.0/grpc-protobuf-1.19.0.jar:/home/steven/.m2/repository/io/grpc/grpc-core/1.19.0/grpc-core-1.19.0.jar:/home/steven/.m2/repository/io/grpc/grpc-context/1.19.0/grpc-context-1.19.0.jar:/home/steven/.m2/repository/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar:/home/steven/.m2/repository/com/google/errorprone/error_prone_annotations/2.2.0/error_prone_annotations-2.2.0.jar:/home/steven/.m2/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar:/home/steven/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.17/animal-sniffer-annotations-1.17.jar:/home/steven/.m2/repository/io/opencensus/opencensus-api/0.19.2/opencensus-api-0.19.2.jar:/home/steven/.m2/repository/io/opencensus/opencensus-contrib-grpc-metrics/0.19.2/opencensus-contrib-grpc-metrics-0.19.2.jar:/home/steven/.m2/repository/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1.jar:/home/steven/.m2/repository/com/google/guava/guava/26.0-android/guava-26.0-android.jar:/home/steven/.m2/repository/org/checkerframework/checker-compat-qual/2.5.2/checker-compat-qual-2.5.2.jar:/home/steven/.m2/repository/com/google/j2objc/j2objc-annotations/1.1/j2objc-annotations-1.1.jar:/home/steven/.m2/repository/com/google/api/grpc/proto-google-common-protos/1.12.0/proto-google-common-protos-1.12.0.jar:/home/steven/.m2/repository/io/grpc/grpc-protobuf-lite/1.19.0/grpc-protobuf-lite-1.19.0.jar:/home/steven/.m2/repository/io/grpc/grpc-stub/1.19.0/grpc-stub-1.19.0.jar:/home/steven/.m2/repository/io/grpc/grpc-netty/1.19.0/grpc-netty-1.19.0.jar:/home/steven/.m2/repository/io/netty/netty-codec-http2/4.1.34.Final/netty-codec-http2-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-common/4.1.34.Final/netty-common-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-buffer/4.1.34.Final/netty-buffer-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-transport/4.1.34.Final/netty-transport-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-resolver/4.1.34.Final/netty-resolver-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-codec/4.1.34.Final/netty-codec-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-handler/4.1.34.Final/netty-handler-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-codec-http/4.1.34.Final/netty-codec-http-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-handler-proxy/4.1.34.Final/netty-handler-proxy-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-codec-socks/4.1.34.Final/netty-codec-socks-4.1.34.Final.jar:/home/steven/.m2/repository/io/netty/netty-tcnative-boringssl-static/2.0.24.Final/netty-tcnative-boringssl-static-2.0.24.Final.jar:/home/steven/.m2/repository/org/axonframework/axon-spring/4.1.1/axon-spring-4.1.1.jar:/home/steven/.m2/repository/org/slf4j/slf4j-api/1.7.26/slf4j-api-1.7.26.jar:/home/steven/.m2/repository/org/axonframework/axon-configuration/4.1.1/axon-configuration-4.1.1.jar:/home/steven/.m2/repository/org/axonframework/axon-disruptor/4.1.1/axon-disruptor-4.1.1.jar:/home/steven/.m2/repository/com/lmax/disruptor/3.4.2/disruptor-3.4.2.jar:/home/steven/.m2/repository/org/axonframework/axon-eventsourcing/4.1.1/axon-eventsourcing-4.1.1.jar:/home/steven/.m2/repository/org/axonframework/axon-modelling/4.1.1/axon-modelling-4.1.1.jar:/home/steven/.m2/repository/org/axonframework/axon-messaging/4.1.1/axon-messaging-4.1.1.jar:/home/steven/.m2/repository/com/thoughtworks/xstream/xstream/1.4.10/xstream-1.4.10.jar:/home/steven/.m2/repository/xpp3/xpp3/1.1.4c/xpp3-1.1.4c.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.1.4.RELEASE/spring-boot-starter-web-2.1.4.RELEASE.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.1.4.RELEASE/spring-boot-starter-json-2.1.4.RELEASE.jar:/home/steven/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.8/jackson-datatype-jdk8-2.9.8.jar:/home/steven/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.8/jackson-datatype-jsr310-2.9.8.jar:/home/steven/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.8/jackson-module-parameter-names-2.9.8.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.1.4.RELEASE/spring-boot-starter-tomcat-2.1.4.RELEASE.jar:/home/steven/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.17/tomcat-embed-core-9.0.17.jar:/home/steven/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.17/tomcat-embed-el-9.0.17.jar:/home/steven/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.17/tomcat-embed-websocket-9.0.17.jar:/home/steven/.m2/repository/org/hibernate/validator/hibernate-validator/6.0.16.Final/hibernate-validator-6.0.16.Final.jar:/home/steven/.m2/repository/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/home/steven/.m2/repository/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/home/steven/.m2/repository/com/fasterxml/classmate/1.4.0/classmate-1.4.0.jar:/home/steven/.m2/repository/org/springframework/spring-web/5.1.6.RELEASE/spring-web-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-beans/5.1.6.RELEASE/spring-beans-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-webmvc/5.1.6.RELEASE/spring-webmvc-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-aop/5.1.6.RELEASE/spring-aop-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-context/5.1.6.RELEASE/spring-context-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-expression/5.1.6.RELEASE/spring-expression-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter-data-jpa/2.1.4.RELEASE/spring-boot-starter-data-jpa-2.1.4.RELEASE.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter-aop/2.1.4.RELEASE/spring-boot-starter-aop-2.1.4.RELEASE.jar:/home/steven/.m2/repository/org/aspectj/aspectjweaver/1.9.2/aspectjweaver-1.9.2.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter-jdbc/2.1.4.RELEASE/spring-boot-starter-jdbc-2.1.4.RELEASE.jar:/home/steven/.m2/repository/com/zaxxer/HikariCP/3.2.0/HikariCP-3.2.0.jar:/home/steven/.m2/repository/org/springframework/spring-jdbc/5.1.6.RELEASE/spring-jdbc-5.1.6.RELEASE.jar:/home/steven/.m2/repository/javax/transaction/javax.transaction-api/1.3/javax.transaction-api-1.3.jar:/home/steven/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar:/home/steven/.m2/repository/javax/activation/javax.activation-api/1.2.0/javax.activation-api-1.2.0.jar:/home/steven/.m2/repository/org/hibernate/hibernate-core/5.3.9.Final/hibernate-core-5.3.9.Final.jar:/home/steven/.m2/repository/javax/persistence/javax.persistence-api/2.2/javax.persistence-api-2.2.jar:/home/steven/.m2/repository/org/javassist/javassist/3.23.1-GA/javassist-3.23.1-GA.jar:/home/steven/.m2/repository/net/bytebuddy/byte-buddy/1.9.12/byte-buddy-1.9.12.jar:/home/steven/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar:/home/steven/.m2/repository/org/jboss/jandex/2.0.5.Final/jandex-2.0.5.Final.jar:/home/steven/.m2/repository/org/dom4j/dom4j/2.1.1/dom4j-2.1.1.jar:/home/steven/.m2/repository/org/hibernate/common/hibernate-commons-annotations/5.0.4.Final/hibernate-commons-annotations-5.0.4.Final.jar:/home/steven/.m2/repository/org/springframework/data/spring-data-jpa/2.1.6.RELEASE/spring-data-jpa-2.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/data/spring-data-commons/2.1.6.RELEASE/spring-data-commons-2.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-orm/5.1.6.RELEASE/spring-orm-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-tx/5.1.6.RELEASE/spring-tx-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-aspects/5.1.6.RELEASE/spring-aspects-5.1.6.RELEASE.jar:/home/steven/.m2/repository/com/h2database/h2/1.4.199/h2-1.4.199.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter/2.1.4.RELEASE/spring-boot-starter-2.1.4.RELEASE.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot/2.1.4.RELEASE/spring-boot-2.1.4.RELEASE.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.4.RELEASE/spring-boot-autoconfigure-2.1.4.RELEASE.jar:/home/steven/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/home/steven/.m2/repository/org/springframework/spring-core/5.1.6.RELEASE/spring-core-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/springframework/spring-jcl/5.1.6.RELEASE/spring-jcl-5.1.6.RELEASE.jar:/home/steven/.m2/repository/org/yaml/snakeyaml/1.23/snakeyaml-1.23.jar:/home/steven/.m2/repository/org/springframework/boot/spring-boot-starter-log4j2/2.1.4.RELEASE/spring-boot-starter-log4j2-2.1.4.RELEASE.jar:/home/steven/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar:/home/steven/.m2/repository/org/apache/logging/log4j/log4j-api/2.11.2/log4j-api-2.11.2.jar:/home/steven/.m2/repository/org/apache/logging/log4j/log4j-core/2.11.2/log4j-core-2.11.2.jar:/home/steven/.m2/repository/org/apache/logging/log4j/log4j-jul/2.11.2/log4j-jul-2.11.2.jar:/home/steven/.m2/repository/org/slf4j/jul-to-slf4j/1.7.26/jul-to-slf4j-1.7.26.jar:/home/steven/.m2/repository/xom/xom/1.3.2/xom-1.3.2.jar:/home/steven/.m2/repository/xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar:/home/steven/.m2/repository/xerces/xercesImpl/2.8.0/xercesImpl-2.8.0.jar:/home/steven/.m2/repository/xalan/xalan/2.7.0/xalan-2.7.0.jar:/home/steven/.m2/repository/com/fasterxml/jackson/module/jackson-module-kotlin/2.9.7/jackson-module-kotlin-2.9.7.jar:/home/steven/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.8/jackson-databind-2.9.8.jar:/home/steven/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.8/jackson-core-2.9.8.jar:/home/steven/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/home/steven/.m2/repository/org/jetbrains/kotlin/kotlin-reflect/1.3.31/kotlin-reflect-1.3.31.jar:/home/steven/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib/1.3.31/kotlin-stdlib-1.3.31.jar:/home/steven/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-common/1.3.31/kotlin-stdlib-common-1.3.31.jar:/home/steven/.m2/repository/org/jetbrains/annotations/13.0/annotations-13.0.jar:/home/steven/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.3.31/kotlin-stdlib-jdk8-1.3.31.jar:/home/steven/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.3.31/kotlin-stdlib-jdk7-1.3.31.jar io.axoniq.demo.bikerental.BikeRentalApplicationKt

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

 INFO --- BikeRentalApplicationKt        : Starting BikeRentalApplicationKt on steven with PID 13418 (/home/steven/workspace/test/bike-rental-demo/target/classes started by steven in /home/steven/workspace/test/bike-rental-demo)
DEBUG --- BikeRentalApplicationKt        : Running with Spring Boot v2.1.4.RELEASE, Spring v5.1.6.RELEASE
 INFO --- BikeRentalApplicationKt        : The following profiles are active: rental
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/home/steven/.m2/repository/com/thoughtworks/xstream/xstream/1.4.10/xstream-1.4.10.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
 INFO --- BikeRentalApplicationKt        : Started BikeRentalApplicationKt in 3.812 seconds (JVM running for 4.434)

It thus seems to start up just fine. So, what's the problem you are trying to solve or already solved?

@smcvb sorry, my mistake.
You also need to go to the BikeService and comment out line 17:
waitFor: EventGateway,

Alright, tried that, with the "desired" exception occurring. Also tried it out in another Spring Boot Axon application.
Key in the issue is the retrieval of an aggregate Repository<A> through the AggregateConfigurer, which hasn't been initialized to the mean Configurer yet.
So yea, a wiring issue, thanks for filing it.

Just as a side note though, a more thorough problem description would've been helpful. My sincere apologies for the wrongly made assumptions, but they were based on the provided description mostly.
That's a lesson for me, but I'd like to ask you to be more 'verbose' in issue description for future occurrences, @Sam-Kruglov.

As of now, I'll reopen this issue as a bug, and update the title and description accordingly.

hello,
is there another workaround (then the kotlin "waitFor": ) for java developers?
I've got the same error on my external command handlers.

Currently i've tried to create the repository-beans manually with the builder classes, but this results in more configuration problems with other parts of the framework, i.e. you have to create your own RepositoryProvider to allow "createNew" and similar problems....looks like an endless pain...

@NiasSt90 it's not specific to kotlin. waitFor is just a name of the field of type EventGateway. The workaround is to "wait for" the gateway to construct, i.e. inject the gateway as well

@Sam-Kruglov ups sry i'm so stupid ;)

after injecting the EventGateway it works. thx.

Hi @smcvb, would you have an update on this issue? I am also getting
Caused by: java.lang.NullPointerException: null
at org.axonframework.config.AggregateConfigurer.lambda$new$11(AggregateConfigurer.java:91) ~[axon-configuration-4.0.jar:4.0]

As the Repository is null
Thanks

Daniel

Please try with Axon 4.3.3. Some similar issues (related to configuration timing) were solved there.

@abuijze the issue is still present in 4.3.3. Can reproduce here by commenting out the queryGateway.

The good news is that Axon is compatible with Spring Boot 2.3.0! 馃槃

With multiple Aggregates i also have to provide a qualifier for each repository. As Spring should be able to support generic wiring since 4.0 it shouldn't throw NoUniqueBeanDefinitionException i think. Intellij also complains that there are no such Beans.

@jnfeinstein I just ran into the same issue. I'm trying to understand how injecting QueryGateway along with Repository helps. In my case, I use QueryGateway but in another bean, so it should be already injected.

I can't find the real root cause of this issue. There is something wrong with the order of how the particular beans are initialized. I think, that one of the dependency for AxonConfiguration (probably Configurer) is built too late. In result, AxonConfiguration#afterPropertiesSet() is not called and we end up with NPE.

I found other, I think more elegant workaround. Instead of injecting QueryGateway next to Repository just mark your repository with @Lazy annotation:

@Autowired @Lazy private Repository<MyAggregate> repository;
Was this page helpful?
0 / 5 - 0 ratings