I have been having a heck of a time with somewhat random circular dependency issues that produce the follow error in the logs:
`
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘categoryController’ defined in URL [jar:file:/app.jar!/BOOT-INF/classes!/com/fedexx/product/category/rest/CategoryController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘queryGateway’ defined in class path resource [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘queryGateway’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘queryBus’ defined in class path resource [org/axonframework/springboot/autoconfig/AxonServerAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.axonserver.connector.query.AxonServerQueryBus]: Factory method ‘queryBus’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘axonMessageInterceptorsConfig’: Unsatisfied dependency expressed through method ‘registerInterceptors’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘commandBus’ defined in class path resource [org/axonframework/springboot/autoconfig/AxonServerAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.axonserver.connector.command.AxonServerCommandBus]: Factory method ‘commandBus’ threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘authCorrelationDataProvider’: Requested bean is currently in creation: Is there an unresolvable circular reference?
`
I never get this error when I deploy the application through my IDE (Spring Tools Suite), but when I deploy the application as a docker container I sometimes get the issue and sometimes I can clear all the cached docker images from my system and rebuild and the problem goes away. But presently I can’t get the issue to go away with this approach and now I’m starting to believe it has less to do with a corrupt docker image layer (which was my previous thought, albeit a little weak) and more to do with the timing of things when the application is deployed as a docker container.
Below is the configuration that defines the authCorrelationDataProvider bean reported in the error above:
`
package com.fedexx;
import static com.fedexx.api.AxonMessageMetadataKeys.USER_INFO;
import java.util.Collections;
import org.axonframework.axonserver.connector.command.AxonServerCommandBus;
import org.axonframework.axonserver.connector.query.AxonServerQueryBus;
import org.axonframework.commandhandling.CommandBus;
import org.axonframework.messaging.Message;
import org.axonframework.messaging.MessageDispatchInterceptor;
import org.axonframework.messaging.MessageHandlerInterceptor;
import org.axonframework.messaging.correlation.CorrelationDataProvider;
import org.axonframework.messaging.correlation.SimpleCorrelationDataProvider;
import org.axonframework.queryhandling.QueryBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert;
import com.fedexx.ResourceServerConfigurer.AuthToken;
import com.fedexx.api.values.UserInfo;
@Configuration
public class AxonMessageInterceptorsConfig {
@Bean
public CorrelationDataProvider authCorrelationDataProvider() {
return new SimpleCorrelationDataProvider(USER_INFO);
}
@Autowired
public void registerInterceptors(CommandBus commandBus, QueryBus queryBus) {
Assert.notNull(commandBus, “Invalid configuration, commandBus is null!”);
Assert.notNull(queryBus, “Invalid configuration, queryBus is null!”);
if (AxonServerCommandBus.class.isAssignableFrom(commandBus.getClass())) {
AxonServerCommandBus.class.cast(commandBus).registerDispatchInterceptor(authorizationDispatchInterceptor());
AxonServerCommandBus.class.cast(commandBus).registerHandlerInterceptor(authorizationHandlerInterceptor());
}
if (AxonServerQueryBus.class.isAssignableFrom(queryBus.getClass())) {
AxonServerQueryBus.class.cast(queryBus).registerDispatchInterceptor(authorizationDispatchInterceptor());
AxonServerQueryBus.class.cast(queryBus).registerHandlerInterceptor(authorizationHandlerInterceptor());
}
}
private MessageDispatchInterceptor<? super Message<?>> authorizationDispatchInterceptor() {
return list -> {
AuthToken auth = (AuthToken) SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
UserInfo userInfo = auth.getPrincipal();
userInfo.validate();
return (index, message) -> message.andMetaData(Collections.singletonMap(USER_INFO, userInfo));
}
return (index, message) -> message;
};
}
private MessageHandlerInterceptor<? super Message<?>> authorizationHandlerInterceptor() {
return (unitOfWork, interceptorChain) -> {
UserInfo userInfo = (UserInfo) unitOfWork.getMessage().getMetaData().get(USER_INFO);
if (userInfo == null) {
throw new SecurityException(“User information not available!”);
}
return interceptorChain.proceed();
};
}
}
`
Anyone else experiencing anything like this?