[英]Injection of resource dependencies failed in spring boot test
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.我假设当我以配置 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;
}
}
Something needs to tell Spring where to find your @Service
beans in order to wire them up.需要告诉 Spring 在哪里可以找到您的
@Service
bean 以便将它们连接起来。 You have told Spring how to find your ConsumerService
bean via您已经告诉 Spring 如何通过以下方式找到您的
ConsumerService
bean
@SpringBootTest(classes = ConsumerService.class)
and you told Spring how to find MyCoolConfig
via你告诉 Spring 如何找到
MyCoolConfig
通过
@ContextConfiguration(classes = {MyCoolConfig.class})
But there is nothing telling Spring how to find CoolTransformer
.但是没有什么告诉 Spring 如何找到
CoolTransformer
。
One way to accomplish this is with a @ComponentScan
annotation.实现此目的的一种方法是使用
@ComponentScan
注释。 That annotation typically goes on a @Configuration
bean, so you might put it on MyCoolConfig
with a package to scan, like so:该注释通常位于
@Configuration
bean 上,因此您可以将其放在MyCoolConfig
上,并使用 package 进行扫描,如下所示:
@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.只要
CoolTransformer
位于com.foo
package 下,Spring 就会找到它并将其连接起来。
If ConsumerService
is also under com.foo
, it will also get scanned, so you don't need to specify it either.如果
ConsumerService
也在com.foo
下,它也会被扫描,所以你也不需要指定它。
Finally, since MyCoolConfig
is now the entrypoint to all your other beans, you can simplify your integration test's annotations to:最后,由于
MyCoolConfig
现在是所有其他 bean 的入口点,您可以将集成测试的注释简化为:
@ExtendWith({SpringExtension.class})
@SpringBootTest(classes = MyCoolConfig.class)
public class EventDecomposerIntegrationTest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.