0

定義されたプロパティを読み取るテストクラスを作成しました

    @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:config/TestOne.xml","classpath*:config/TestTwo.xml"
})
public class PropertyTest {
        @Value("${test.one:DEFAULT}")
        private String test;
        @Value("${test.two:DEFAULT}")
        private String test2;

        @Test
        public void TestProperty(){

                System.out.println(test + "," + test2);

        }

}

TestOne.xml

  <context:property-placeholder
                location="classpath*:/config/testone.properties"                
                ignore-unresolvable="true" order="1" />  

TestTwo.xml

<context:property-placeholder
                location="classpath*:/config/testtwo.properties"               
                ignore-unresolvable="true" order="2" />

testone.properties

test.one=testone

testtwo.プロパティ

test.one=testone

test.two=test

テストの実行中、出力は

テストトーン,DEFAULT

プロパティから test.two を取得していません。

デフォルト値を指定していない場合

@Value("${test.two}")
        private String test2;

出力は testone,test

誰でも助けることができますか?

4

1 に答える 1

0

この問題: 「2 つのプロパティ プレースホルダーとデフォルト値」は簡単に解決できないのではないかと心配していproperty-placeholderます。また"。

property-placeholder回避策は、2 つの構成ファイルと最高の順序を持​​つ1 つだけを持つことです。

<context:property-placeholder
            locations="classpath*:/config/testone.properties,
                       classpath*:/config/testtwo.properties"                
            ignore-unresolvable="true" order="0" />  

(注意:locationsの代わりにプロパティlocation)

于 2016-02-28T07:46:18.643 に答える