gpt4 book ai didi

java - 如何在 Spring 中使用自定义 BeanName 创建 bean?

转载 作者:行者123 更新时间:2023-12-04 07:21:05 25 4
gpt4 key购买 nike

问题
有一个Processor处理基于其 typesToProcess 的类:

import org.springframework.stereotype.Component;

@Component
public class Processor {

private String typesToProcess;

public Processor(String typesToProcess) {
this.typesToProcess = typesToProcess;
}

public void process(String type) {
if (typesToProcess.equals(type)) {
// process
}
}
}
我需要创建一些 Processor实例在不同的地方工作。问题是我不知道有多少 Processor应该在 Spring 应用程序引导时创建实例,以及它们的“typesToProcess”。更进一步, Processor的数量应用程序运行时,实例可能会发生变化。我需要定期从存储在数据库中的一些配置文件中读取类型。
尝试
我可以解决这个问题的一种方法是使用 typesToProcess + Processor作为 bean 名,虽然我知道它不会工作。有没有其他办法?
import com.meituan.picture.selection.processor.impl.Processor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ProcessorContainer implements ApplicationContextAware {

private ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

public Processor getProcessor(String type) {
String realBeanName = "processor" + type;
// obviouslly this wont work
return applicationContext.getBean(realBeanName, Processor.class);
}

}

最佳答案

我通过将 bean 的范围指定为 prototype 解决了类似的问题不使用bean名称。下面的例子解释了我的实现:
创建您的 Processor类如下图:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import lombok.Getter;
import lombok.Setter;

@Component
@Getter
@Setter
@Scope("prototype")
public class Processor {
private String typesToProcess;

public Processor(String typesToProcess) {
this.typesToProcess = typesToProcess;
}

public void process(String type) {
if (typesToProcess.equals(type)) {
// process
}
}
}
实现 ApplicationContextAware在运行时生成 bean:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

@Service
public class ApplicationContextAwareImpl implements ApplicationContextAware {

private static ApplicationContext context;

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
ApplicationContextAwareImpl.initApplicationContext(applicationContext);
}

private static void initApplicationContext(ApplicationContext applicationContext) {
ApplicationContextAwareImpl.context = applicationContext;
}

public static <T> T getBean(Class<T> requiredType) {
return context.getBean(requiredType);
}

public static Processor getProcessor(String typesToProcess) {
Processor processor = ApplicationContextAwareImpl.getBean(Processor.class);
processor.setTypesToProcess(typesToProcess);

return processor;
}
}
使用 ApplicationContextAwareImpl.getProcessor()以编程方式生成bean的方法:
Processor type1Processor = ApplicationContextAwareImpl.getProcessor("type1");

关于java - 如何在 Spring 中使用自定义 BeanName 创建 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68495739/

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