从配置文件(properties)中读取配置项

配置文件:(src/main/resources/config.properties)

hsf.container.path=D:/usr/share/hsf
id=7628bd2634914d2ebeff0d6cd19b642c

//调用配置文件

public class Timing {


    private Properties propertie = new Properties();
    private IReviewService reviewService = null;
    
    public static void main(String args[]) throws Exception{
        Timing timing = new Timing();
        timing.init();
        timing.run();
        System.exit(0);
    }
    
    public void init() throws Exception {
        String configPath = "/config.properties";//配置文件的加载位置
        try {
            InputStream in = this.getClass().getResourceAsStream(configPath);
            propertie.load(in);
            String path = propertie.getProperty("hsf.container.path");
            HSFContainerDelegator.start(path);
            Thread.sleep(5000); // 保障HSF容器完整启动
            ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "demo.application.xml");
            reviewService = (IReviewService)appContext.getBean("reviewService");
        } catch (Exception e) {
            System.out.println("[Error init!]" + "get config exception"+e.getMessage());
            e.printStackTrace();
        }
    }
    
    public void run(){
        String id = propertie.getProperty("id");
        QueryById qid = new QueryById(id);
        Review reivew = reviewService.load(qid);
        System.out.println("This is log demo");
        System.out.println("Load review by id from propertyies is null?"+(null == reivew));
    }
    
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!对于从 properties 文件读取配置,您可以使用 Java 的 Properties 类来实现。以下是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ConfigReader { private Properties properties; public ConfigReader(String filePath) { properties = new Properties(); try { FileInputStream fileInputStream = new FileInputStream(filePath); properties.load(fileInputStream); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public String getProperty(String key) { return properties.getProperty(key); } public static void main(String[] args) { ConfigReader configReader = new ConfigReader("config.properties"); String username = configReader.getProperty("db.username"); String password = configReader.getProperty("db.password"); System.out.println("Username: " + username); System.out.println("Password: " + password); } } ``` 上述代码创建了一个 ConfigReader 类,通过构造函数传入 properties 文件的路径,并提供了一个 getProperty 方法用于获取指定键的配置值。在 main 方法,我们创建了一个 ConfigReader 对象,并从配置文件获取了数据库的用户名和密码。 至于从数据库读取配置,可以使用 JDBC 连接数据库,并执行 SQL 查询来获取相应的配置值。以下是一个简单的示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBConfigReader { private Connection connection; public DBConfigReader(String url, String username, String password) { try { connection = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } } public String getProperty(String key) { String value = null; try { Statement statement = connection.createStatement(); String query = "SELECT value FROM config WHERE key = '" + key + "'"; ResultSet resultSet = statement.executeQuery(query); if (resultSet.next()) { value = resultSet.getString("value"); } statement.close(); } catch (SQLException e) { e.printStackTrace(); } return value; } public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydb"; String username = "root"; String password = "password"; DBConfigReader dbConfigReader = new DBConfigReader(url, username, password); String apiKey = dbConfigReader.getProperty("api.key"); System.out.println("API Key: " + apiKey); } } ``` 上述代码创建了一个 DBConfigReader 类,通过构造函数传入数据库的 URL、用户名和密码,提供了一个 getProperty 方法用于执行 SQL 查询并获取指定键的配置值。在 main 方法,我们创建了一个 DBConfigReader 对象,并从数据库获取了 API 密钥。 请注意,以上只是示例代码,您需要根据实际情况进行适当的修改和调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值