1. XML中读取配置文件
<!-- 数据库配置文件读取 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:com/cmcc/sleepgw/Config/database.properties</value>
</list>
</property>
</bean>
<!-- 数据库驱动的配置 --><!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
</bean>
2. java中读取
import java.io.InputStream;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
public class PropertiesReader {
private static Properties prop = new Properties();
static
{
try {
InputStream SystemIn = new ClassPathResource("com/cmcc/sleepgw/Config/SysConf.properties").getInputStream();
InputStream SystemIn2 = new ClassPathResource("com/cmcc/sleepgw/Config/database.properties").getInputStream();
prop.load(SystemIn);
prop.load(SystemIn2);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getProp(String name)
{
if(prop!=null)
{
return (String) prop.get(name);
}
return null;
}
public static void main(String[] args)
{
System.out.println(PropertiesReader.getProp("sleepDataRoot"));
System.out.println(PropertiesReader.getProp("jdbc.username"));
}
}