'Spring boot application does not load configuration class from another base package

I have configuration and a service in com.foo.cloud.bar package, and it is bundled as JAR only (no spring boot stuff) for reuse across all apps writing data to Kafka.

package com.foo.cloud.bar;

@Configuration
public class KafkaHandlerConfiguration {
...
}

and

package com.foo.cloud.bar;
@Service
public interface SwiftalkKafkaGateway {

    /**
     * @param message {@link Message} object to publish to the Gateway
     */
    @Async
    void publish(Message<?> message);
}

Now I want to use it in a spring boot application

package com.foo.cloud.baz;
@SpringBootApplication(scanBasePackages = {"com.foo.cloud.bar", "com.foo.cloud.baz"})
@EnableReactiveMongoAuditing
@OpenAPIDefinition(info = @Info(
        title = "Collaboration Microservices",
        version = "v1",
        description = "Comments and Attachments Microservice"))
public class CollaborationMicroserviceApplication {
...
}

And then I autowire the gateway class to a component in this application

package com.foo.cloud.baz.comments.model;

@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CommentMongoEventListener extends AbstractMongoEventListener<Comment> {

    private final SwiftalkKafkaGateway gateway;

...
}

But none from scanBasePackages, @ComponentScan nor @Import help as my tests fail to run with the error

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.foo.cloud.bar.SwiftalkKafkaGateway' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source