我将数据库配置写到一个 jdbc.properties 的配置文件里面但是我忘了 在spring配置文件里面如何 读取jdbc.properties 里面的东西

解决方案 »

  1.   

    网上搜一下,spring属性文件外置
      

  2.   


    <bean id="extendedPropertyPlaceholderConfigurer" class="com.scommon.util.properties.ExtendedPropertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>classpath:hibernate.properties</value>
    <value>classpath:global.properties</value>
    </list>
    </property>
    </bean><property name="jdbcUrl" value="${url}"/>
    <!-- 指定连接数据库的用户名 -->
    <property name="user" value="${username}"/>
    <!-- 指定连接数据库的密码 -->
    <property name="password" value="${password}"/><property name="jdbcUrl" value="${url}"/>
    <!-- 指定连接数据库的用户名 -->
    <property name="user" value="${username}"/>
    <!-- 指定连接数据库的密码 -->
    <property name="password" value="${password}"/>
      

  3.   


    package com.scommon.util.properties;import java.util.Properties;import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class ExtendedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{ private Properties props;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
    throws BeansException {
    super.processProperties(beanFactoryToProcess, props);
    this.props = props;
    }

    public Object getProperty(String key) {
    return props.get(key);
    }
    }