gpt4 book ai didi

java - 相同的数据应用于相同类型的所有 Bean,同时创建具有不同数据的 Bean 列表

转载 作者:太空宇宙 更新时间:2023-11-04 10:25:06 25 4
gpt4 key购买 nike

我的用例是创建一个 Bean“B”列表,每个 Bean 都有一个不同的依赖 bean“C”实例。

@Component("a")
public class A{

List<B> bPool = new ArrayList<>();
private ApplicationContext appContext;

@Autowired
A(ApplicationContext appContext){
this.appContext = appContext;
}

@PostConstruct
public void init(){
for(int i=0; i<POOL_SIZE; i++){
bPool.add((B) appContext.getBean("b"));
}
}

//code for multi-threading, which uses beans from the list bPool.
//I iterate the list, launch multiple threads,
//pass different data to each thread and combine results.
List<CompletableFuture> multiThreads = new ArrayList<>();
Iterator it = bPool.iterator();
for(Data d : listOfSomeData){ //same size for listOfSomeData and bPool
CompletableFuture var = CompletableFuture.supplyAsync(() -> {
B curr = (B) it.next()
curr.someMethodInB(d);
});
multiThreads.add(var);
}
multiThreads.forEach(cf -> cf.join());
}

@Component("b")
@Scope("prototype")
public class B{
//Service class - has some dependencies, like C below
private C c;
private ApplicationContext appContext;

@Autowired
B(ApplicationContext appContext){
this.appContext = appContext;
}

@PostConstruct
public void init(){
c = (C) appContext.getBean("c");
}

}

@Component("c")
@Scope("prototype")
public class C{
//this class holds some data and does some processing on it,
//I want this to be different for different instances in different threads.
}

在 bPool 中创建 B 列表时,在构造时(我通过在构造后打印进行检查),分别为每个 B 设置了不同的 C 实例。

但是,当稍后使用同一池中的 B 实例时,所有 B 实例都具有相同的 C 实例。

它们都有 C 实例,该实例被设置为 bPool 的最后创建的 B 元素。

我是 springboot 的新手,无法理解这种行为。任何帮助表示赞赏。谢谢

最佳答案

仍不确定导致问题的原因。然而,另一种方法解决了我的用例。

我继续使用了 spring 本身提供的异步功能。

引用:documentation link

  1. 在主类中,添加 @EnableAsync 注释并创建要运行的后台线程池。

    @SpringBootApplication
    @EnableAsync
    public class Application {

    public static void main(String[] args) {
    SpringApplication.run(Application.class, args).close();
    }

    @Bean
    public Executor asyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(2);
    executor.setMaxPoolSize(2);
    executor.setQueueCapacity(500);
    executor.setThreadNamePrefix("ThreadName-");
    executor.initialize();
    return executor;
    }
    }
  2. 使用带有 @Async 注释的公共(public)方法创建一个新类,其中包含我们要在后台运行的繁重代码。此方法需要是公共(public)的,并且必须位于不同的类中才能使代理正常工作。我们无法在调用该方法的同一个类中创建此异步方法。

    @Component
    public class MyAsyncService {

    @Async
    public CompletableFuture<ResultClass> myAsyncMethod(String params){
    //do some heavy tasks here
    return CompletableFuture.completedFuture(instanceOfResultClass);
    }
    }
  3. 从调用者类中使用异步方法调用上述类。

    @Component
    public class CallerService{

    private MyAsyncService myAsyncService;

    @Autowired
    public CallerService(MyAsyncService myAsyncService){
    this.myAsyncService = myAsyncService;
    }

    public void myMethod(){
    CompletableFuture<ResultClass> result1 = myService.findUser("PivotalSoftware");
    CompletableFuture<ResultClass> result2 = gitHubLookupService.findUser("CloudFoundry");

    CompletableFuture.allOf(result1, result2).join();

    ResultClass finalResult1 = result1.get();
    ResultClass finalResult2 = result2.get();
    }
    }

关于java - 相同的数据应用于相同类型的所有 Bean,同时创建具有不同数据的 Bean 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50638178/

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