【问题标题】:Import java.io.File variable in another class在另一个类中导入 java.io.File 变量
【发布时间】:2020-04-15 13:47:37
【问题描述】:

我在一个 Java 项目中有两个包。

Package A 有一个名为App.java 的类。此类接受参数解析(利用 apache commons-cli lib)。

代码是

CommandLineParser clip = new DefaultParser();
Options options = new Options();
options.addOption("zp", "ZipFilePath", true, "Mention the path where zip file is present");
options.addOption("d", "dbPropFile", true, "Mention the path database property file");
options.addOption("h", "help", false, "This mentions how to use this utility");
CommandLine cli = clip.parse(options, args);
File dbpropFile = new File(cli.getOptionValue("d"));

现在如何在包 B 的另一个类 (SQLServerConn.java) 中导入 dbPropFile 变量?

package com.abc.integra.db.B;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import com.abc.integra.db.A.LoadDBProps;


public class SQLServerConn {

    public static Connection dbConn;
    public static Properties props;

    public static Connection getConn() {

        try {

            final String drivername = (String)SQLServerConn.props.get("drivername");
            final String url = (String)SQLServerConn.props.get("url");
            final String dbname = (String)SQLServerConn.props.get("dbname");
            final String username = (String)SQLServerConn.props.get("username");
            final String password = (String)SQLServerConn.props.get("password");
            //jasypt final String password1 = (String)SQLServerConn.props.getProperty(key);
            Class.forName(drivername); //registering the driver before connection with the DB
            SQLServerConn.dbConn = DriverManager.getConnection(url + ";databaseName=" + dbname + ";user=" + username + ";password=" + password);
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }

        return SQLServerConn.dbConn;
    }

    static {

        SQLServerConn.dbConn = null;
        try {
            final LoadDBProps lp = new LoadDBProps();
            SQLServerConn.props = lp.loadDBProperties(filename);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我想在加载属性部分用dbpropFile 变量值代替filename

【问题讨论】:

    标签: java file-io java-io apache-commons-cli


    【解决方案1】:

    您必须提供导入类的完整路径。例如:

    import xx.A.App;
    

    现在您可以访问 App 类,您可以使用以下任一方法:

    1) App 对象访问它的 public 类成员 (composition),

    class App {
      File dbPropFile;
      CommandLineParser clip = new DefaultParser();
      public void setDbPropFile (String fileOption) {
           this.dbPropFile = new File(clip.getOptionValue(fileOption));
      }
    
      public File getDbPropFile () {
           return dbPropFile;
      }
    }
    
    public class SQLServerConn {
      App app = new App();
      app.setDbPropFile("d");
      SQLServerConn.props = lp.loadDBProperties(app.getDbPropFile());
    }
    

    2) 直接类名访问staticpublic类成员。

    class App {
      static File dbpropFile = new File(cli.getOptionValue("d"));
    }
    
    class SQLServerConn  {
       SQLServerConn.props = lp.loadDBProperties(App.dbpropFile); // exact class name to access static variable
    }
    

    如果您要求仅导入单个属性dbpropFile,则在 java 中是不可能的。

    【讨论】:

    • 嗨 @Zain Ul Abideen ...所以如果我导入 App,我可以使用 dbpropFile 变量吗?
    猜你喜欢
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2021-04-16
    • 2017-08-30
    相关资源
    最近更新 更多