5

通常、次のようにプロパティ名を知っている場合は、注釈を使用してフィールドにデータを入力します。

@Value("${myproperties.myValue}")
private String myString

ただし、名前が不明な場合は、ファイル内のすべてのプロパティをループして、値と名前の両方を保存したいと思います。春とJavaの最良の方法は何ですか?

4

4 に答える 4

33

実際、ファイルからプロパティを読み取るだけで、Springのプロパティプレースホルダーでこれらのプロパティを使用する必要がない場合、解決策は簡単です。

public class Test1 {
    @Autowired
    Properties props;

    public void printProps() {
        for(Entry<Object, Object> e : props.entrySet()) {
            System.out.println(e);
        }
    }

..。

<util:properties id="props" location="/spring.properties" />
于 2013-01-03T10:29:01.390 に答える
1

この@ValueメカニズムPropertyPlaceholderConfigurerは、を介して機能しBeanFactoryPostProcessorます。使用されるプロパティは実行時に公開されません。考えられる解決策については、私の以前の回答を参照してください。

于 2013-01-03T09:41:57.320 に答える
1

これよりも簡単な解決策は見つかりませんでした

class PropertyPlaceholder extends PropertyPlaceholderConfigurer {
    Properties props;

    @Override
    protected Properties mergeProperties() throws IOException {
        props = super.mergeProperties();
        return props;
    }
}

public class Test1 {
    @Autowired
    PropertyPlaceholder pph;

    public void printProps() {
        for(Entry<Object, Object> e : pph.props.entrySet()) {
            System.out.println(e);
        }
    }

    ...

..。

<bean class="test.PropertyPlaceholder">
    <property name="locations">
        <value>/app.properties</value>
    </property>
</bean>
于 2013-01-03T10:05:17.937 に答える
-1

列挙を使用してプロパティをループする

于 2016-04-12T22:33:26.963 に答える