我使用Spring Boot创建了一个用作kafka客户端的库,实际上,库中只有一些类,每个类都带有@SpringBootConfiguration
和@EnableAutoConfiguration
注释。
@Slf4j
@SpringBootConfiguration
@EnableAutoConfiguration
public class KafkaHandlerConfiguration {
...
}
和
@Service
interface SwiftalkKafkaGateway {
...
}
我创建了一个带有依赖项的jar,这个JAR将通过CDI在Java EE webapp中使用。我将通过以下代码在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 boot自动配置启动bean所需的属性?我既有spring.kafka
属性组,也有通过KafkaHandlerConfiguration
中的@Value
注释注入的自定义属性
@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",
发布于 2021-04-02 07:04:30
使用ConfigurableEnvironment
和MutablePropertySource
对我很有效;下面是我如何将环境加载到上下文中
@Singleton
@ApplicationScoped
class SwiftalkAnnotatedSpringContextLoader {
private final AnnotationConfigApplicationContext springContext;
SwiftalkAnnotatedSpringContextLoader() throws IOException {
springContext = new AnnotationConfigApplicationContext();
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
Properties appProps = new Properties();
appProps.load(this.getClass().getClassLoader().getResourceAsStream("spring-config.properties"));
propertySources.addFirst(new PropertySource<Properties>("spring-properties", appProps) {
@Override
public Object getProperty(String name) {
return appProps.getProperty(name);
}
});
springContext.setEnvironment(environment);
springContext.scan("com.digite.cloud.swiftalk");
springContext.refresh();
}
ApplicationContext getSwiftalkKafkaClientContext() {
return this.springContext;
}
}
在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);
ThreadPoolTaskExecutor asyncExecutor = loader.getSwiftalkKafkaClientContext().getBean(
ThreadPoolTaskExecutor.class);
Assertions.assertTrue(asyncExecutor.getCorePoolSize() == 20);
}
在spring boot库中,corePoolSize
的缺省值为10
@Value("${digite.swiftalk.kafka.executor.core-pool-size:10}")
private Integer corePoolSize;
https://stackoverflow.com/questions/66883426
复制