一、spring加载配置文件
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:ldap.properties</value>
</property>
</bean>
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="${url}" />
<property name="base" value="${base}" />
<property name="userDn" value="${userDn}" />
<property name="password" value="${password}" />
</bean>
二、java加载静态文件:
1. 在包中新建一个类 Config.java
package Project;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Config {
public static String CONNECTION_TYPE;
public static String CONNECTION_URL;
public static String CONNECTION_USER;
public static String CONNECTION_PWD;
public static String CONNECTION_DRIVER;
public static String FILE_STORE_PATH;
public static String FTP_IP;
public static String FTP_USERNAME;
public static String FTP_PASSWORD;
public static String FTP_PORT;
public static String FTP_CHOICE_STORE;
public static String FTP_CHOICE_AUTO;
private static Properties prop = new Properties();
static {
try {
prop.load(Config.class.getResourceAsStream("DatabaseProperties.properties"));
} catch (IOException e) {
e.printStackTrace();
}
//get the data from the .prop file
CONNECTION_TYPE = prop.getProperty("conn_type");
CONNECTION_URL = prop.getProperty("conn_url");
CONNECTION_USER = prop.getProperty("conn_userName");
CONNECTION_PWD = prop.getProperty("conn_passWord");
CONNECTION_DRIVER = prop.getProperty("conn_driver");
FILE_STORE_PATH = prop.getProperty("file_store_path");
FTP_IP = prop.getProperty("ftp_ip");
FTP_USERNAME = prop.getProperty("ftp_userName");
FTP_PASSWORD = prop.getProperty("ftp_passWord");
FTP_PORT = prop.getProperty("ftp_port");
FTP_CHOICE_STORE = prop.getProperty("ftp_choice_store");
FTP_CHOICE_AUTO = prop.getProperty("ftp_choice_auto");
}
//write the data to .prop file
public static void setProperty(String filePath, String parameterKey, String parameterValue){
Properties prop = new Properties();
try{
InputStream in = new FileInputStream(filePath);
prop.load(in);
FileOutputStream ou = new FileOutputStream(filePath);
prop.setProperty(parameterKey, parameterValue);
prop.store(ou, filePath);
}catch(IOException e){
e.printStackTrace();
}
}
}
2. 新建一个 DatabaseProperties.properties 文件,内容如下:
conn_driver=test_1
ftp_choice_auto=test_2
ftp_ip=test_3
ftp_passWord=test_4
conn_userName=test_5
ftp_choice_store=test_6
file_store_path=test_7
conn_type=test_8
conn_passWord=test_9
ftp_userName=test_10
conn_url=test_11
ftp_port=test_12
3. 写一个测试的例子:Test.java
package Project;
public class Test {
public static void main(String args[]){
// get the data from .prop file
System.out.println(Config.CONNECTION_DRIVER);
String s1="new test";
// set the new data for .prop file
Config.setProperty("src\\Project\\DatabaseProperties.properties", "conn_driver", s1);
// show the newest data from .prop file
System.out.println(Config.CONNECTION_DRIVER);
}
}