[英]Read all properties files in a directory
現在我在春天讀屬性文件
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="customer/messages" />
</bean>
在這里,我指定在客戶目錄中讀取messages.properties。 但是我要做的是指定一個目錄,並要求spring讀取該目錄中存在的所有屬性文件。 我該如何實現?
我嘗試過value =“ customer / *”,但是不起作用。
推薦使用<context:property-placeholder>
作為:
<context:property-placeholder
locations="classpath:path/to/customer/*.properties" />
您也可以使用Spring 3.1+ Java Config來執行此操作:
@Bean
public static PropertyPlaceholderConfigurer properties(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[ ]
{ new ClassPathResource( "path/to/customer/*.properties" ) };
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
return ppc;
}
您可能需要定制資源類型以從以下項加載屬性:
要使用屬性,可以使用Environment
抽象。 可以將其注入並用於在運行時檢索屬性值。
在以下博客上給出的最終使用方法-http: //rostislav-matl.blogspot.in/2013/06/resolving-properties-with-spring.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.