动态修改Spring数据源信息
1: ApplicationConfig.properties
?
? ?jdbc.driverClassName=com.mysql.jdbc.Driver
? ?jdbc.url=jdbc:mysql://192.168.1.192:3306/xxx?useUnicode=true&characterEncoding=UTF-8
? ?jdbc.username=xxxx
? ?jdbc.password=xxx
?
2:DBInfo.java
?
package test;
?
public class DBInfo {
? ? ? ?private String driverName;
? ? ? ?private String url;
? ? ? ?private String userName;
? ? ? ?private String passWord;
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
?
}
?
3:测试Test.java
?
package test;
?
import java.util.ResourceBundle;
?
public class Test {
?
/**
* @param args
*/
private static String driverName;?
? ? private static String url;
? ? private static String userName;
? ? private static String passWord;
? ? public static DBInfo method(String filePath){?
? ? ? ? DBInfo db = new DBInfo();
? ? ? ? ResourceBundle rb=ResourceBundle.getBundle(filePath);
? ? ? ? driverName = rb.getString("jdbc.driverClassName");
? ? ? ? db.setDriverName(driverName);
? ? ? ? url = rb.getString("jdbc.url");
?
db.setUrl(url);
? ? ? ? userName = rb.getString("jdbc.username");
? ? ? ? db.setUserName(userName);
? ? ? ? passWord = rb.getString("jdbc.password");
? ? ? ? db.setPassWord(passWord);
? ? ? ? return db;
? ? }
public static void main(String[] args) {
DBInfo info=method("conf.ApplicationConfig");
System.out.println(info.getDriverName());
System.out.println(info.getUrl());
System.out.println(info.getUserName());
System.out.println(info.getPassWord());
?
}
?
}
?
?
?
?
?