我使用springboot创建了一个库作为kafka客户机使用,实际上库中只有类,每个类都用 @SpringBootConfiguration
以及 @EnableAutoConfiguration
注解。
@Slf4j
@SpringBootConfiguration
@EnableAutoConfiguration
public class KafkaHandlerConfiguration {
...
}
和
@Service
interface SwiftalkKafkaGateway {
...
}
我为它创建了一个带有依赖项的jar,这个jar将通过cdi在javaeewebapp中使用。我将通过以下代码获得cdi上下文中的bean
@Singleton
@ApplicationScoped
class SwiftalkAnnotatedSpringContextLoader {
private final AnnotationConfigApplicationContext springContext;
SwiftalkAnnotatedSpringContextLoader() {
springContext = new AnnotationConfigApplicationContext();
springContext.scan("com.digite.cloud.swiftalk");
springContext.refresh();
}
ApplicationContext getSwiftalkKafkaClientContext() {
return this.springContext;
}
}
如何传递spring引导自动配置启动bean所需的属性?我两者都有 spring.kafka
通过注入的一组属性和自定义属性 @Value
中的注解 KafkaHandlerConfiguration
```
@Value("${digite.swiftalk.kafka.executor.core-pool-size:10}")
private Integer corePoolSize;
@Value("${digite.swiftalk.kafka.executor.max-pool-size:20}")
private Integer maxPoolSize;
@Value("${digite.swiftalk.kafka.executor.queue-capacity:100}")
private Integer queueCapacity;
和
"spring.kafka.producer.properties.max.block.ms=1000",
"spring.kafka.producer.bootstrap-servers=localhost:9999",
"spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer",
"spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer",
1条答案
按热度按时间7gcisfzg1#
使用
ConfigurableEnvironment
以及MutablePropertySource
为我工作;下面是如何将环境加载到上下文中在中添加了文件
src/test/resources
```spring.data.mongodb.database=embedded
spring.data.mongodb.port=12345
spring.data.mongodb.host=localhost
spring.kafka.producer.properties.max.block.ms=2000
spring.kafka.producer.bootstrap-servers=localhost:19092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
digite.swiftalk.kafka.upstream-type-header=UPSTREAM-TYPE
digite.swiftalk.kafka.upstream-instance-header=INSTANCE-HEADER
digite.swiftalk.kafka.message-key-header=MESSAGE-KEY-HEADER
digite.swiftalk.kafka.executor.core-pool-size=20
digite.swiftalk.kafka.executor.max-pool-size=50
digite.swiftalk.kafka.executor.queue-capacity=1000
@Test
void testLoadsSpringApplicationContext() throws IOException {
SwiftalkAnnotatedSpringContextLoader loader = new SwiftalkAnnotatedSpringContextLoader();
SwiftalkKafkaGateway kafkaGateway = loader.getSwiftalkKafkaClientContext().getBean(SwiftalkKafkaGateway.class);
assertNotNull(kafkaGateway);
}
@Value("${digite.swiftalk.kafka.executor.core-pool-size:10}")
private Integer corePoolSize;