Spring PropertyPlaceholderConfigurer的使用和自定义

一、PropertyPlaceholderConfigurer的使用

<!--在applicationContext.xml文件中 -->
<
bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:dataSource.properties</value> </property> </bean>
####在dataSource.properties文件中

Jdbc_Driver=com.mysql.jdbc.Driver Jdbc_URL=jdbc\:mysql\://localhost\:3306/graduate_design Jdbc_User=root Jdbc_Password=admin
<!-- 在applicationContext.xml中可以用${param} 的形式进行引用 -->
<property name="driverClass" value="${Jdbc_Driver}" />
        <property name="jdbcUrl" value="${Jdbc_URL}" />
        <property name="user" value="${Jdbc_User}" />
        <property name="password" value="${Jdbc_Password}" />
<!-- 当然如果是两个变量还可以进行叠加使用 如下:
file_dir_home是文件主目录
file_dir_upload是文件上传目录(相对主目录的路径)
在properties文件中有如下定义:
#default home directory
file_dir_home=d\:\\\\GraduateDesign\\\\
#default upload file directory based on home directory
file_dir_upload=file\\\\upload\\\\
-->
<property name="fileDirUpload" value="${file_dir_home}${file_dir_upload}" />

 

二、PropertyPlaceholderConfigurer加载多个Properties文件

当然有时候需要将多个properties文件一起加载到Spring中,可以如下操作:

<bean id="configBean"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:dataSource.properties</value>
                <value>classpath:basicConstant.properties</value>
            </list>
        </property>
    </bean>

将location变为locations并添加list即可。

三、自定义CustomizedPropertyPlaceholderConfigurer及使用

当然有时候也需要自定义PropertyPlaceholderConfigurer,而且希望在其他的Servlet或者Action中来使用configBean中的变量,而不是仅仅在applicationContext.xml文件中使用,于是可以通过继承的方式来使用。

public class CustomizedPropertyPlaceholderConfigurer extends
        PropertyPlaceholderConfigurer {

    
    private  Map<String, Object> propertyMap; 
     
    @Override 
    protected void processProperties( ConfigurableListableBeanFactory beanFactoryToProcess, 
            Properties props) throws BeansException { 
        super.processProperties(beanFactoryToProcess, props); 
        propertyMap = new HashMap<String, Object>(); //此处添加HashMap来保存这些properties的K,V键值对
        for (Object key : props.keySet()) { 
            String keyStr = key.toString(); 
            String value = props.getProperty(keyStr); 
            propertyMap.put(keyStr, value); 
        }  
    } 
 
    public  Object getContextProperty(String name) { 
        return propertyMap.get(name); 
    } 
}

在配置文件中会相应的调整为

<bean id="configBean"
        class="nlp.util.CustomizedPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:dataSource.properties</value>
                <value>classpath:basicConstant.properties</value>
            </list>
        </property>
    </bean>

将class调整为自定义的类;

然后在其他的Servlet或者Action给中可以通过利用WebApplicationContextUtils来加载这个WebApplicationContext中的Spring bean的方式来进行取值:

     //Action中获取方式
    //ServletContext servletContext =  ServletActionContext.getServletContext();  
    //Servlet中获取方式
    ServletContext servletContext = this.getServletContext();  
        WebApplicationContext context =   
                    WebApplicationContextUtils.getWebApplicationContext(servletContext);  
        CustomizedPropertyPlaceholderConfigurer configBean = 
                (CustomizedPropertyPlaceholderConfigurer) context.getBean("configBean");
        String fileDirUpload = (String) configBean.getContextProperty("file_dir_home")
                + configBean.getContextProperty("file_dir_upload");

 

posted @ 2015-12-23 12:46  √珞珈搬砖工√  阅读(342)  评论(0)    收藏  举报