如何在 spring 中创建一个 bean 池?

How to create a pool of bean in spring?

在我的项目中,我需要调用其他一些应用程序 SOAP url 来获取连接,在该连接对象之上,我需要传递各种请求以获得响应。我需要解析此响应以获得实际结果数据。

有一个 API 称为 Retrival class 对象时保持连接。需要使用相同的连接,即传递请求和获取响应对象。

所以我可以将其声明为原型范围以获取这些 Retrival 对象。 但是我正在使用 Akka Actors,它使用这些对象获取不同的数据。

我无法使用原型作用域创建未知数量的 Retrival class bean。稍后可能会导致 OOM 异常。

我想创建一些 Retrival class 的 20 个 bean 并使用它们。 我如何在 spring 中执行此操作,我使用的是 spring 最新版本。

我尝试使用 commonspool2targetsource,但是通过从其他应用程序 jar 导入的 Retrival class...无法识别 class 路径中的 Retrival.class。

不确定该怎么做,非常感谢您的帮助。

这是我正在做的一段示例代码:

@Bean(name="retriver")
    @Scope("prototype")
    public Retriver retriver() {

        Retriver dr = null;
        try {
            dr = new Retriver(config.getUserName(),
                    config.getPassword(),
                    config.getUrl());
        } catch (Exception e) {
            logger.error("Exepction in creating SOAP datasource connection");
        } 

        return dr;
    }


    @Bean
    public CommonsPool2TargetSource RetriverPooledTargetSource() {
        final CommonsPool2TargetSource commonsPoolTargetSource = new CommonsPool2TargetSource();
        commonsPoolTargetSource.setTargetBeanName("Retriver");
        commonsPoolTargetSource.setTargetClass(Retriver.class); //Getting error here....
        commonsPoolTargetSource.setMaxSize(10);
        return commonsPoolTargetSource;
    }

    @Bean
    public ProxyFactoryBean proxyFactoryBean() {
        ProxyFactoryBean p = new ProxyFactoryBean();
        p.setTargetSource(RetriverPooledTargetSource());
        return p;
    }

错误: 无法解析类型 org.apache.commons.pool2.PooledObjectFactory。它是从所需的 .class 个文件

中间接引用的

第 2 部分问题:

更改为低于公共池 2 ..之前的错误消失了。

<dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>          
    </dependency>

但是当我说 pool.getActive 时,如下所示显示计数为 0

CommonsPool2TargetSource pool  = configurationUtils.retrieverPooledTargetSource();
        System.out.println( " pool.getActiveCount " +  pool.getActiveCount());

没有例外,但是当我检查上面的池对象时。 有一个名为 creationException

的文件

内容如下:

java.lang.Exception
    at org.apache.commons.pool2.impl.BaseGenericObjectPool.<init>(BaseGenericObjectPool.java:147)
    at org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:105)
    at org.springframework.aop.target.CommonsPool2TargetSource.createObjectPool(CommonsPool2TargetSource.java:225)
    at org.springframework.aop.target.CommonsPool2TargetSource.createPool(CommonsPool2TargetSource.java:205)
    at org.springframework.aop.target.AbstractPoolingTargetSource.setBeanFactory(AbstractPoolingTargetSource.java:83)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1767)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1732)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:394)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:366)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f.dataRetrieverPooledTargetSource(<generated>)
    at com.spgmi.ca.prescore.config.ConfigurationUtils.proxyFactoryBean(ConfigurationUtils.java:60)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f.CGLIB$proxyFactoryBean(<generated>)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f$$FastClassBySpringCGLIB$1ac14b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f.proxyFactoryBean(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:620)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:974)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:848)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:565)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:514)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:485)
    at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:227)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1403)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.spgmi.ca.prescore.PreScoreProducerApplication.main(PreScoreProducerApplication.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

问题3

什么时候调用这个

   CommonsPool2TargetSource pool  = configurationUtils.dataRetrieverPooledTargetSource();

    dataRetriever  = (DataRetriever) pool.getTarget();
    System.out.println(" " + dataRetriever);

它抛出以下异常:我在这里做错了什么?

原因:java.lang.IllegalStateException:没有可用的 Commons ObjectPool 在 org.springframework.util.Assert.state(Assert.java:73) 在 org.springframework.aop.target.CommonsPool2TargetSource.getTarget(CommonsPool2TargetSource.java:234) 在 com.spgmi.ca.prescore.actor.CompanyTransIndustryActor.preStart(CompanyTransIndustryActor.java:63) 在 akka.actor.Actor$class.aroundPreStart(Actor.scala:510) 在 akka.actor.UntypedActor.aroundPreStart(无类型Actor.scala:95) 在 akka.actor.ActorCell.create(ActorCell.scala:590) ... 还有 9 个

您需要添加以下依赖项(这是用于 maven 构建):

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>          
    </dependency>