没有限定的bean,用@configurationproperties注解

xggvc2p6  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(441)

我使用@configurationproperties配置属性:

@Getter
@Setter
@ConfigurationProperties("kafka")
public class KafkaConnectionSettings {
     private String bootstrapAddress = "dataflow-kafka:9092";
{

并尝试在其他配置类中自动连接它。例如:

@Configuration
@AllArgsConstructor
public class KafkaProducerConfig {
    private KafkaConnectionSettings kafkaSettings;

@Bean
public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(
       ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,kafkaSettings.getBootstrapAddress());
   }
}

但我得到以下例外:

No qualifying bean of type 'com.app.config.kafka.KafkaConnectionSettings' available: expected single matching bean but found 2: kafkaConnectionSettings,kafka-com.app.config.kafka.KafkaConnectionSettings

考虑到在“com.app.config”包中我有一个类似的配置,这有点奇怪:

@Getter
 @Setter
 @ConfigurationProperties("app")
public class FileSettings {
}

最后一个authowired在类中成功地用@service注解
我做错什么了?

z0qdvdin

z0qdvdin1#

问题解决了。我忘了在testclass的@enableconfigurationproperties中添加kafkanConnectionSettings.class

@ActiveProfiles("test")
@EnableConfigurationProperties({FileSettings.class, KafkaConnectionSettings.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {

对不起,这个问题不太清楚。

相关问题