我是一个相对较新的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;
}
我需要能够配置A.x、B.y、B.z、B.w、B.clist、C.v和C.l
我有一个问题与每个新线程的B初始化有关,我不知道在编译时clist是否会保持为空,只有在运行时我才能知道将创建多少线程。对于每一个新线程,我都用相同的配置创建新的B。
(我查看了autowire和原型功能,我怀疑它可能会有所帮助)
编辑
这里我有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>
我有一个可能的解决方案来解决这个问题,它可能不是最优的。
在xml中的A bean中,我可以注释掉clist的设置(根本不包括在结构中),
当我在java代码中创建Bbean时,我可能每次都使用getBean("B"),同时将xml中的Bbean定义为原型。
像这样:
<!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"))
}
}