0

我有一堂课如下:

public class SensitivityDescription {

    private final String measureId;
    private final ColumnType type;  private final String sensName;
    private final SensType sensType;
    private final ServicePhase svcPhase;
    private final AggregateFunction agFn;
    private final String positionEnum;

    private static List <SensitivityDescription> senRecordList = new ArrayList<SensitivityDescription> ();

    public SensitivityDescription(String measureId, ColumnType type,
    String sensName, SensType sensType, ServicePhase svcPhase,
    AggregateFunction agFn, String positionEnum) {
    super();
    this.measureId = measureId;
    this.type = type;
    this.sensName = sensName;
    this.sensType = sensType;
    this.svcPhase = svcPhase;
    this.agFn = agFn;
    this.positionEnum = positionEnum;
}

我需要用类的static senRecordList对象填充SensitivityDescription。我如何在 spring xml 中执行此操作。

4

2 回答 2

0

Spring 的思维方式与您的方法不符。
但是你可以解决这个问题。
为 SenRecordList 添加一个公共设置器用于 SensitivityDescription。添加默认构造函数

public class SensitivityDescription {

    private final String measureId;
    ...

    private static List <SensitivityDescription> senRecordList = new ArrayList<SensitivityDescription> ();

    public SensitivityDescription(String measureId,..) {
    ...
    }
    /** new code */
   public SensitivityDescription() {super();}   
   public void setSenRecordList(List<SensitivityDescription> senRecordList) {
        SensitivityDescription.senRecordList = senRecordList;
   }

}

实例化 bean 一次,这样列表将被注入到您的字段中。

<bean class="...SensitivityDescription">
    <property name="senRecordList">
    <util:list  value-type="...SensitivityDescription">
        <bean class="...SensitivityDescription">
            <constructor-arg name="name" “measureId”
                <value>id1</value>
                    </constructor-arg>
            <constructor-arg name="name" "sensName"
                <value>name1</value>
                    </constructor-arg>
                ...
        </bean>
        <bean class="...SensitivityDescription">
            <constructor-arg name="name" "measureId"
                <value>id2</value>
                    </constructor-arg>
            <constructor-arg name="name" "sensName"
                <value>name2</value>
                    </constructor-arg>
                ...
        </bean>     
    </util:list>
    </property>
    </bean>

如果这不是遗留代码并且您可以更改它,我建议为这些值定义一个单例而不是静态字段。春天会更喜欢它。

于 2013-07-09T10:58:54.010 回答
0

基于此链接,一切正常:

班级

package com.sopovs.moradanen.test;

import java.util.List;

public class SensitivityDescription {

private String name;

private static List<SensitivityDescription> senRecordList;


public SensitivityDescription(String name) {

    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


public static void setSenRecordList(List<SensitivityDescription> senRecordList) {
    SensitivityDescription.senRecordList = senRecordList;
}

public static List<SensitivityDescription> getSenRecordList() {
    return senRecordList;
}

}

xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"     xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/util     http://www.springframework.org/schema/util/spring-util.xsd">


<bean id="first" class="com.sopovs.moradanen.test.SensitivityDescription">
    <constructor-arg name="name" value="first" />
</bean>

<bean id="second" class="com.sopovs.moradanen.test.SensitivityDescription">
    <constructor-arg name="name" value="second" />
</bean>

<bean id="third" class="com.sopovs.moradanen.test.SensitivityDescription">
    <constructor-arg name="name" value="third" />
</bean>


<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod"
        value="com.sopovs.moradanen.test.SensitivityDescription.setSenRecordList" />
    <property name="arguments">
        <list>
            <ref bean="first" />
            <ref bean="second" />
            <ref bean="third" />
        </list>
    </property>
</bean>

</beans>

并测试:

package com.sopovs.moradanen.test;


import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:test.xml")
public class SpringTest {

@Test
public void test() {
    assertEquals(3, SensitivityDescription.getSenRecordList().size());

}
}
于 2013-07-09T06:51:24.133 回答