【发布时间】:2011-11-30 00:36:13
【问题描述】:
想知道是否有一种方法可以使用 PropertyPlaceholderConfigurer 类根据属性文件中的一组值动态实例化 bean。
我有一个 java bean 说 Student 有两个属性:“name”和“subject”
我有一个属性文件:
student.1.name=student1name
student.1.subject=student1subject
student.2.name=student2name
student.2.name=student2subject
现在我有一个可以获取学生列表的 Classroom 对象。
我想知道是否有一种方法可以使用 Spring 来做到这一点。这里的挑战是学生人数可能会有所不同。
如果只有一个学生对象,那么:
<bean id="student" class="com.abc.Student">
<property name="name" value="${student.1.name}" />
<property name="subject"
value="${student.1.subject}" />
</bean>
<bean id="classRoom" class="com.abc.ClassRoom">
<property name="student" ref="student" />
</bean>
本来可以的。但在这种情况下,我们有一个包含 n 个学生的列表。 n 的值可能会根据属性文件中的条目数而有所不同。
【问题讨论】:
标签: spring properties javabeans