java—在非spring管理的web应用程序中向spring boot自动配置类注入属性

nvbavucw  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(366)

我使用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",

7gcisfzg

7gcisfzg1#

使用 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);

}

的默认值 `corePoolSize` 在spring boot库中是10

@Value("${digite.swiftalk.kafka.executor.core-pool-size:10}")
private Integer corePoolSize;

相关问题