NullPointerException on `config` in `AxonConfiguration`

Hi,

I’m experiencing a NullPointerException on config in AxonConfiguration when the JpaEventStorageEngine is created in the following spring configuration:

`
import org.axonframework.common.jdbc.PersistenceExceptionResolver;
import org.axonframework.common.jpa.EntityManagerProvider;
import org.axonframework.common.transaction.TransactionManager;
import org.axonframework.config.SagaConfiguration;
import org.axonframework.eventhandling.EventBus;
import org.axonframework.eventhandling.EventMessage;
import org.axonframework.eventhandling.interceptors.EventLoggingInterceptor;
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.eventsourcing.eventstore.jpa.JpaEventStorageEngine;
import org.axonframework.messaging.MessageHandlerInterceptor;
import org.axonframework.messaging.interceptors.LoggingInterceptor;
import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.upcasting.event.EventUpcaster;
import org.axonframework.spring.config.AxonConfiguration;
import org.axonframework.spring.eventhandling.scheduling.java.SimpleEventSchedulerFactoryBean;
import org.axonframework.spring.eventsourcing.SpringAggregateSnapshotterFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import my.saga.PricingSaga;

import java.util.function.Function;

@Configuration
public class AxonConfig {

// @Autowired
// public void configure(List<SagaConfiguration<?>> sagaConfigurations) {
// sagaConfigurations.forEach(sc -> sc.registerHandlerInterceptor(c -> new LoggingInterceptor<>()));
// }

@Bean(name = “eventBus”)
public EmbeddedEventStore eventStore(EventStorageEngine storageEngine) {
EmbeddedEventStore eventStore = new EmbeddedEventStore(storageEngine);
eventStore.registerDispatchInterceptor(new EventLoggingInterceptor());
return eventStore;
}

@Bean
public EventStorageEngine eventStorageEngine(Serializer serializer,
PersistenceExceptionResolver persistenceExceptionResolver,
AxonConfiguration configuration,
EntityManagerProvider entityManagerProvider,
TransactionManager transactionManager) {
return new JpaEventStorageEngine(
serializer,
configuration.getComponent(EventUpcaster.class), // !!HBD!! //
persistenceExceptionResolver,
5, //batchSize -> defaults to 100 if null
entityManagerProvider,
transactionManager,
1L, //lowestGlobalSequence -> defaults to 1 if null
100, //maxGapOffset -> defaults to 10000 if null
true);
}

@Bean
public SimpleEventSchedulerFactoryBean eventScheduler(EventBus eventBus,
PlatformTransactionManager transactionManager) {
SimpleEventSchedulerFactoryBean eventSchedulerFactory = new SimpleEventSchedulerFactoryBean();
eventSchedulerFactory.setEventBus(eventBus);
eventSchedulerFactory.setTransactionManager(transactionManager);
return eventSchedulerFactory;
}

@Bean
public SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean() {
return new SpringAggregateSnapshotterFactoryBean();
}

@Bean
public SagaConfiguration pricingSagaConfiguration() {
return SagaConfiguration.trackingSagaManager(PricingSaga.class)
.registerHandlerInterceptor(loggingInterceptor(PricingSaga.class)); // ?HBD? //
}

/**
*Some beans, including aggregates and repositories, omitted for brevity
*/

private Function<org.axonframework.config.Configuration, MessageHandlerInterceptor<? super EventMessage<?>>> loggingInterceptor(Class loggerName) {
return (config) -> new LoggingInterceptor<>(loggerName.getName());
}
}
`

The step that’s failing is configuration.getComponent(EventUpcaster.class), due to the config object in AxonConfiguration still being null at this point, apparently because afterPropertiesSet() has not yet been called:

@Override public void afterPropertiesSet() throws Exception { config = configurer.buildConfiguration(); }

What’s most confusing is that removing the LoggingInterceptor from the SagaConfiguration makes this problem disappear

How should I correctly set up these beans, considering the spring lifecycle?

I would expect a bean being injected into another to have been initialized.
Since you are creating the EventStorageEngine bean yourself, you probably already know whether you have an EventUpcaster in your application, or not. If so, you could wire it directly using Spring, instead of using the AxonConfiguration to provide one.

Cheers,

Allard