我正在使用在spring.xml中初始化的List。我需要使用propertyPlaceholderConfigurer来松散耦合pointList.cfg.properties文件中的初始值。
这是spring.xml代码:
<bean id="parentTriangle" class="edu.akarimin.koushik.Triangle" autowire="byName" init-method="myInit" destroy-method="cleanUp">
<property name="pointList">
<list>
<ref bean="pointA" />
</list>
</property>
</bean>
<bean id="triangle" class="edu.akarimin.koushik.Triangle" parent="parentTriangle">
<property name="pointList">
<list merge="true">
<ref bean="pointB" />
<ref bean="pointC" />
<ref bean="pointD" />
</list>
</property>
</bean>
<bean id="pointA" class="edu.akarimin.koushik.Point">
<property name="x" value="${pointA.pointX}" />
<property name="y" value="${pointA.pointY}" />
</bean>
<bean id="pointB" class="edu.akarimin.koushik.Point">
<property name="x" value="${pointB.pointX}" />
<property name="y" value="${pointB.pointY}" />
</bean>
<bean id="pointC" class="edu.akarimin.koushik.Point">
<property name="x" value="${pointC.pointX}" />
<property name="y" value="${pointC.pointY}" />
</bean>
<bean id="pointD" class="edu.akarimin.koushik.Point">
<property name="x" value="${pointD.pointX}" />
<property name="y" value="${pointD.pointY}" />
</bean>
<bean id="pointE" class="edu.akarimin.koushik.Point">
<property name="x" value="${pointE.pointX}" />
<property name="y" value="${pointE.pointY}" />
</bean>
<bean class="org.springframework.beans.factory.config.propertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:pointList.cfg.properties</value>
</list>
</property>
</bean>
三角类代码是:
package edu.akarimin.koushik; 公共类Triangle {
private List<Point> pointList;
public void draw(){
for (Point point : pointList) {
System.out.println("Point= (" +point.getX()+ ","+point.getY()+")");
}
public void myInit(){
System.out.println("MyInit method called for Triangle");
}
public void cleanUp(){
System.out.println("CleanUp method called for Triangle");
}
主要方法是:
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = context.getBean("triangle", Triangle.class);
triangle.draw();
}
}
class Point是:
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
有任何解决问题的想法吗?