gpt4 book ai didi

java - spring从非bean类访问代理bean

转载 作者:行者123 更新时间:2023-11-30 06:06:14 45 4
gpt4 key购买 nike

我有一个 spring bean,它在其方法上使用 dropwizard-metrics 注释来测量一些指标(请参阅#parse 方法):

@Component
public class Consumer extends AbstractConsumer {

@Autowired
private EventsService eventsService;

private ExecutorService executor;

@PostConstruct
void init() throws Exception {
executor = Executors.newFixedThreadPool(1);

ConsumerEventsHandler consumer = new ConsumerEventsHandler(Arrays.asList("topic1"), this);
executor.submit(consumer);
}

@Timed(name = CONSUMER_BATCH_PARSING_TIME, absolute = true)
public void parse(ConsumerRecord<String, String> record) {
String val = record.value();
eventsService.parseToDB(val);
}
}

正如您所看到的,这个 bean 向执行程序服务提交了一个新任务,该任务不是一个 bean,但它需要对 Consumer bean 的引用来重用其逻辑(请参阅 consumer.解析(记录);调用):

public class ConsumerEventsHandler implements Runnable {

private Consumer consumer;
private List<String> topics;
protected final KafkaConsumer<String, String> kafkaConsumer;

public ConsumerEventsHandler(List<String> topics, Consumer consumer) {
this.topics = topics;
this.consumer = consumer;
this.kafkaConsumer = new KafkaConsumer<>();
}

@Override
public void run() {
try {
consumer.subscribe(topics);
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);

for (ConsumerRecord<String, String> record : records) {
consumer.parse(record);
}
}
} finally {
consumer.close();
}
}

}

这是指标配置(我使用“ryantenney/metrics-spring”lib):

@Configuration
@EnableMetrics(proxyTargetClass = true)
@ComponentScan(basePackages = "com.inq")
public class SsvpApiMetricsConfig extends MetricsConfigurerAdapter {
@Resource
private MetricRegistry registry;

@PostConstruct()
public void init() {
configureReporters(registry);
}

@Override
public void configureReporters(MetricRegistry metricRegistry) {

registerReporter(JmxReporter.forRegistry(metricRegistry).
inDomain("com.inq.metrics").build()).start();
}
}

结果,我看到 Consumer bean 已被代理,但 ConsumerEventsHandler 包含对普通 bean 的引用,因为它是在 Consumer 的内创建的> 通过 'new ConsumerEventsHandler(..)' 的 @PostConstruct 方法 Consumer bean 尚未被代理,我假设 @PostConstruct 在代理事物之前被调用。

我看到的唯一解决方法是每次在 #run 方法内通过 ApplicationContext#getBean 获取Consumer bean ref,而不是将引用存储在 ConsumerEventsHandler 类变量中。还有其他解决办法吗?

最佳答案

只需在非 Spring 类上创建一个静态

public class ConsumerEventsHandler implements Runnable {

private static Consumer consumer;
private List<String> topics;
protected final KafkaConsumer<String, String> kafkaConsumer;

public ConsumerEventsHandler(List<String> topics) {
this.topics = topics;
this.kafkaConsumer = new KafkaConsumer<>();
}

public static setConsumer(Consumer consumer) {
ConsumerEventsHandler.consumer = consumer;
}

然后在你的 Spring 一侧你可以有:

  @PostConstruct()
public void init() {
configureReporters(registry);
ConsumerEventsHandler.setConsumer(consumer);
}

关于java - spring从非bean类访问代理bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265829/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com