简体   繁体   English

在不同类别之间共享对象

[英]sharing object across different classes

I need some "design" advise. 我需要一些“设计”建议。 I have static JDBC objects and my "main entry class" which are shared among other classes. 我有静态JDBC对象和“主条目类”,它们在其他类之间共享。

My MainClass looks like this: 我的MainClass看起来像这样:

public static Jdbc db1;
public static Jdbc db2;

connectDb(makeDirectConnection) // depending on runtime passed argument

public static connectDb(boolean makeDirectConnection) {
if(makeDirectConnection) // use direct connection
    db1 = JdbcFactory.getInstance("db/config/main/db1.properties");
    db2 = JdbcFactory.getInstance("db/config/main/db2.properties");
} else { // connect using via SSH tunnel (different host and port)
    db1 = JdbcFactory.getInstance("db/config/tunnel/db1.properties");
    db2 = JdbcFactory.getInstance("db/config/tunnel/db2.properties");
}

JdbcFactory maintains Map of instances. JdbcFactory维护实例Map

It kinda works ok, but if I want to make unit test for classes where db1 or db2 is being used I get null pointer exception if from unit test I don't do MainClass.dbConnect() 挺好的,但是如果我想对正在使用db1db2类进行单元测试,则如果从单元测试中不执行MainClass.dbConnect()则会得到空指针异常

Make thing worse - from test classes I need even one more different DB setup, so from Test.class I do: 更糟糕的是-从测试类中,我什至需要一个不同的数据库设置,因此在Test.class我需要:

Main.db1 = JdbcFactory.getInstance("db/config/test/db1.properties");

All together it's messy and I don't like. 总的来说,这很混乱,我不喜欢。 Isn't there some nicer approach how to share db1 and db2 ? 没有更好的方法共享db1db2吗?

Also boolean makeDirectConnection which is defined from java run argument stops me from using final db1 and db2 . 另外,从java run参数定义的boolean makeDirectConnection阻止我使用final db1db2 Any advice how to workaround this? 任何建议如何解决此问题? (It depends on environment where program is executed - but I don't want to make it dependable on hostname or some other OS thing. (这取决于执行程序的环境-但我不想使其依赖于主机名或其他操作系统。

I would not provide a boolean as the jvm argument to differ between your two (or three) cases. 我不会提供布尔值作为jvm参数来区别您的两种(或三种)情况。 Instead i would provide the db urls via paramter. 相反,我将通过参数提供数据库URL。 In that case you can also call your method within your test with the "test-db.properties". 在这种情况下,您还可以使用“ test-db.properties”在测试中调用您的方法。

Since you are need to set the default instance diffrently for testing and deployment. 由于需要为测试和部署分别设置默认实例。 So create a property file which mentiones that which file should be used to create default instance for db1 and db2 . 因此,创建一个属性文件 ,其中提到应使用哪个文件来创建db1db2默认实例。

To remove the need of calling MainClass.dbConnect() from your unit test code, just create a static block and here initlize db1 and db2 with default. 要消除从单元测试代码中调用MainClass.dbConnect()的需要,只需创建一个静态块,然后在此处使用默认值初始化db1db2 eg If property file is defaultDB.properties and having following content: 例如,如果属性文件是defaultDB.properties并且具有以下内容:

DB1=db/config/test/db1.properties
DB2=db/config/test/db2.properties

then use following: 然后使用以下命令:

static private Properties prop;
static {
    prop = new Properties();
    prop.load(new FileInputStream("defaultDB.properties"));
    db1 = JdbcFactory.getInstance(prop.getProperty("DB1"));
    db2 = JdbcFactory.getInstance(prop.getProperty("DB2"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM