Reputation: 1232
The following relevant signatures I've defined below. I'm running an integration test of mine and getting this failure:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consumerService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'CoolTransformer' available
I assume the context is being loaded as I target the config class. What could I be missing? I have a service annotation with a name on the service that is failing to load.. I've tried autowired as well with a qualifier.
@ExtendWith({SpringExtension.class})
@SpringBootTest(classes = ConsumerService.class)
@ContextConfiguration(classes = {MyCoolConfig.class})
public class EventDecomposerIntegrationTest
@Service("CoolTransformer")
public class CoolTransformer extends BaseTransformer<GraphComponents> {
@Service
public class ConsumerService {
@Resource(name = "CoolTransformer")
private BaseTransformer coolTransformer;
@Configuration
public class MyCoolConfig {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Value("${spring.kafka.schema-registry}")
private String schemaRegistry;
@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
return props;
}
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs());
}
@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>>
kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
factory.getContainerProperties().setMissingTopicsFatal(false);
return factory;
}
}
Upvotes: 2
Views: 8428
Reputation: 2880
Something needs to tell Spring where to find your @Service
beans in order to wire them up. You have told Spring how to find your ConsumerService
bean via
@SpringBootTest(classes = ConsumerService.class)
and you told Spring how to find MyCoolConfig
via
@ContextConfiguration(classes = {MyCoolConfig.class})
But there is nothing telling Spring how to find CoolTransformer
.
One way to accomplish this is with a @ComponentScan
annotation. That annotation typically goes on a @Configuration
bean, so you might put it on MyCoolConfig
with a package to scan, like so:
@Configuration
@ComponentScan("com.foo")
public class MyCoolConfig {
As long as CoolTransformer
is somewhere under the com.foo
package, Spring will find it and wire it up.
If ConsumerService
is also under com.foo
, it will also get scanned, so you don't need to specify it either.
Finally, since MyCoolConfig
is now the entrypoint to all your other beans, you can simplify your integration test's annotations to:
@ExtendWith({SpringExtension.class})
@SpringBootTest(classes = MyCoolConfig.class)
public class EventDecomposerIntegrationTest
Upvotes: 1