通常、次のようにプロパティ名を知っている場合は、注釈を使用してフィールドにデータを入力します。
@Value("${myproperties.myValue}")
private String myString
ただし、名前が不明な場合は、ファイル内のすべてのプロパティをループして、値と名前の両方を保存したいと思います。春とJavaの最良の方法は何ですか?
通常、次のようにプロパティ名を知っている場合は、注釈を使用してフィールドにデータを入力します。
@Value("${myproperties.myValue}")
private String myString
ただし、名前が不明な場合は、ファイル内のすべてのプロパティをループして、値と名前の両方を保存したいと思います。春とJavaの最良の方法は何ですか?
実際、ファイルからプロパティを読み取るだけで、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" />
この@Value
メカニズムPropertyPlaceholderConfigurer
は、を介して機能しBeanFactoryPostProcessor
ます。使用されるプロパティは実行時に公開されません。考えられる解決策については、私の以前の回答を参照してください。
これよりも簡単な解決策は見つかりませんでした
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>