题目:从配置文件config.propeties中读取数据
1. 使用Configuration接口获取配置文件中的数据
2. 方法和变量都添加static关键字,让这个类在加载后直接执行getFileInfo方法,其它类可以直接通过静态变量调用配置文件中的数据。
package everyday; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.BeforeClass; import org.junit.Test; public class ConfigInfo { public static String dbaddress; public static String username; public static String password; public static String datapath; @BeforeClass public static void getFileInfo() throws ConfigurationException { Configuration config = new PropertiesConfiguration("conf/config.properties"); dbaddress = config.getString("dbaddress"); username = config.getString("username"); password = config.getString("password"); datapath = config.getString("datafilepath"); } }
时间: 2024-11-03 22:05:13