1. 程式人生 > >spring 屬性檔案的載入,支援正則表示式

spring 屬性檔案的載入,支援正則表示式

pe1.在Spring 中可以使用以下兩個類載入資原始檔:
org.springframework.context.support.ResourceBundleMessageSource

org.springframework.context.support.ReloadableResourceBundleMessageSource
後者可以不重起伺服器的情況下,讀取資原始檔,所以可以隨時更改資原始檔。
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>message</value>
            </list>
        </property>
    </bean>
測試:
  Object[] args = {"我", "你"};
        String msg = applicationContext.getMessage("hello", args, Locale.CHINA);
        System.out.println("msg=" + msg);
如果利用applicationContext.getMessage(xxx),則id只能為“messageSource”。因為這裡的Class需要填入MessageSource介面的實現。        
但可以直接用:
((AbstractMessageSource)applicationContext.getBean("messageSource")).getMessage(xxxx)
支援萬用字元的載入屬性檔案:
import java.util.Locale;
public abstract class AbstractMessageResources {
    public String getMessage(String key) {
    return getMessage(key, Locale.getDefault());
    }
    public String getMessage(String key, Object param) {
    return getMessage(key, new Object[] {param}, null, Locale.getDefault());
    }
    public String getMessage(String key, Object[] params) {
    return getMessage(key, params, Locale.getDefault());
    }
    public String getMessage(String key, Object[] params, Locale locale) {
    return getMessage(key, params, null, locale);
    }
    public String getMessage(String key, Locale locale) {
    return getMessage(key, null, null, locale);
    }
    public String getMessage(String key, Object param, Locale locale) {
    return getMessage(key, new Object[] {param}, null, locale);
    }
    
    public abstract String getMessage(String key, Object[] params, String defaultMessage, Locale locale);
}
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class RegexPropertyMessageResources extends AbstractMessageResources {
    
    static Log logger = LogFactory.getLog(RegexPropertyMessageResources.class);
    
    public static String PROPERTY_POSTFIX = ".properties";
    //載入屬性資原始檔
   // private ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
 private ReloadableResourceBundleMessageSource resourceBundleMessageSource = new ReloadableResourceBundleMessageSource();    
    //支援萬用字元匹配 
    private PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
    public RegexPropertyMessageResources() {
    
    }
    public RegexPropertyMessageResources(String baseName) {
        setBaseNames(new String[] { baseName });
    }
    public RegexPropertyMessageResources(String[] baseNames) {
        setBaseNames(baseNames);
    }
    public void setBaseName(String baseName) {
        setBaseNames(new String[] { baseName });
    }
    
    public void setBaseNames(String[] baseNames) {
        List<String> baseNameList = new ArrayList<String>();
        try {
            for(String baseName : baseNames) {
                Resource[] resources = patternResolver.getResources(baseName);   //通過萬用字元取得到所有對應的source
                for(Resource resource : resources) {
                String fileName = resource.getFilename();
                baseNameList.add(fileName.substring(0, fileName.indexOf(PROPERTY_POSTFIX)));  
                //除掉後的.properties字尾,因為直接用ResourceBundleMessageSource,是不用加字尾的
                if(logger.isInfoEnabled()) {
                    logger.info("Add properties file: [" + resource.getDescription() + "]");
                }
                }
            }        
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
        this.resourceBundleMessageSource.setBasenames(baseNameList.toArray(new String[baseNameList.size()]));
    }
    //取得屬性對應的資訊
    public String getMessage(String key, Object[] params, String defaultMessage, Locale locale) {
        return this.resourceBundleMessageSource.getMessage(key, params, defaultMessage, locale);
    }
}
    <bean id="regexPropertyMessageResources" class="test.RegexPropertyMessageResources">
        <property name="baseNames">
            <list>
                <value>message*.properties</value>
            </list>
        </property>
    </bean>
    @Test
    public void loadProp3(){
        Object[] args = {"我", "你"};
        String msg = ((AbstractMessageResources)super.getBean("regexPropertyMessageResources")).getMessage("hello", args, Locale.CHINA);
        assertEquals("我and你",msg);
         
    }