0

我是一个相对较新的 Spring 用户,我有兴趣使用这个框架来加载复杂的嵌套配置。

以下是我的架构设计的伪代码:

   class **A** implements runnable{
      int x;
      Collection<**B**> clist;         //not fixed size list, 
      run(){
           // if (something happens) 
           //           new Thread(**B**(y,z,w))
      }
   } 

   class **B** implements runnable{
      int y;
      int z;
      int w;
      Array<**C**> bclist;            // fixed size array of C known at init time
      run(){
           process...
      }
   } 

   class **C**{
      int v;
      int l;
   }

我需要能够配置 Ax、By、Bz、Bw、B.clist、Cv 和 Cl

我有一个与每个新线程的 B 初始化相关的问题,我在编译时不知道 clist 是否会保持为空,只有在运行时我才会知道将创建多少个线程。对于每个新线程,我都会创建具有相同配置的新 B。

(我查看了自动装配和原型功能,我怀疑它可能会有所帮助)

编辑

这里我有xml示例文件:

            <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
            <beans>
                <bean id="A" class="...">
                    <property name="x" value="150" />
                    <!-- HERE IS MY PROBLEM-->

                    <property name="clist">
                        <list>
                            <ref bean="B" />
                        </list>
                </bean>

                <bean id="B" class="...">
                    <property name="y" value="20" />
                    <property name="z" value="7" />
                    <property name="w" value="7" />
                    <property name="bclist">
                        <list>
                            <ref bean="C" />
                        </list>
                </bean>

                <bean id="C" class="...">
                    <property name="v" value="3" />
                    <property name="l" value="1" />
                </bean>
            </beans>
4

1 回答 1

0

我对这个问题有一个可能的解决方案,它可能不是最佳的。

在 xml 中的 A bean 中,我可以注释掉 clist 的设置(结构中根本不包括它),

当我在java代码中创建B bean时,我可能每次都使用getBean(“B”),同时将xml中的B bean定义为原型。

像这样 :

            <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
            <beans>
                <bean id="A" class="...">
                    <property name="x" value="150" />
                </bean>

                <bean id="B" class="..." scope="prototype">
                    <property name="y" value="20" />
                    <property name="z" value="7" />
                    <property name="w" value="7" />
                    <property name="bclist">
                        <list>
                            <ref bean="C" />
                        </list>
                </bean>

                <bean id="C" class="...">
                    <property name="v" value="3" />
                    <property name="l" value="1" />
                </bean>
            </beans>

Java代码:

  class **A** implements runnable{
    int x;
    Collection<**B**> clist;         //not fixed size list, 
    run(){
       // if (something happens) 
       //           new Thread((B)context.getBean("B"))
    }
  } 
于 2012-08-27T09:46:19.533 回答