我正在嘗試創建注釋以啟用自定義 Kafka 配置以構建公共庫。我對這種方法的想法是為我的所有應用程式輕松配置 kafka,洗掉所有樣板配置。
所以我想用注釋來注釋我的主要應用程式類,并為 kafka 偵聽器和發布器進行所有配置。
但是我的配置類在 spring 組件之后初始化,我得到錯誤:Consider defining a bean of type 'org.springframework.kafka.core.KafkaTemplate' in your configuration
我的注釋:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import(KafkaListenerConfigurationSelector.class)
public @interface CustomEnableKafka {}
我的配置選擇器:
public class KafkaListenerConfigurationSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{CustomKafkaAutoConfiguration.class.getName()};
}
}
最后是我的配置類:
@Slf4j
@Configuration
@EnableConfigurationProperties(CustomKafkaPropertiesMap.class)
@AutoConfigureBefore({KafkaAutoConfiguration.class})
@RequiredArgsConstructor
public class CustomKafkaAutoConfiguration {
//my properties comming from application.yml
private final CustomKafkaPropertiesMap propertiesMap;
private final ConfigurableListableBeanFactory configurableListableBeanFactory;
@PostConstruct
public void postProcessBeanFactory() {
// My logic to register beans
propertiesMap.forEach((configName, properties) -> {
// Configuring my factory with a bean name: myTopicKafkaProducerFactory
var producerFactory = new DefaultKafkaProducerFactory<>(senderProps(properties));
configurableListableBeanFactory.registerSingleton(configName "KafkaProducerFactory", producerFactory);
//Configuring my kafka template with a bean name: myTopicKafkaTemplate
var kafkaTemplate = new KafkaTemplate<>(producerFactory);
configurableListableBeanFactory.registerSingleton(configName "KafkaTemplate", kafkaTemplate);
});
}
}
我不知道我怎么能優先考慮這個配置而不是另一個。
編輯:
當我@Autowired使用限定符在客戶配置中注冊的任何 bean時myTopicKafkaTemplate,例如:
@Service
public class TestService {
@Autowired
@Qualifier("myTopicKafkaTemplate")
private KafkaTemplate<String, Object> myTopicKafkaTemplate;
}
然后我收到一條錯誤訊息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field myTopicKafkaTemplate in com.example.demo.service.TestService required a bean of type 'org.springframework.kafka.core.KafkaTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
uj5u.com熱心網友回復:
自動配置不是那樣作業的。它不能被匯入,也不能像你那樣做所有自動配置的事情@AutoConfigureBefore({KafkaAutoConfiguration.class})。
CustomKafkaAutoConfiguration考慮在檔案中將其配置META-INF/spring.factories為像這樣的條目,而不是自定義注釋:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.my.project.CustomKafkaAutoConfiguration
編輯
感謝您分享有關您的配置的更多資訊!
因此,為了@Autowired使其作業,必須在應用程式背景關系中注冊具有相應名稱和型別的 bean 定義。但是,您CustomKafkaAutoConfiguration本身就是 bean,它會在其 bean 初始化階段注冊這些單例,這對于自動裝配候選人來說已經很晚了。
考慮實施ImportBeanDefinitionRegistrar而不是@PostConstruct. 它不能再是@Configuration,也不能@EnableConfigurationProperties再好。您可以將它移到一個單獨的@Configuration類中,但是在ImportBeanDefinitionRegistrar您必須使用 an EnvironmentAware- 您不能從ImportBeanDefinitionRegistrar.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/536399.html
標籤:爪哇春天弹簧靴弹簧卡夫卡
