我有一个 build.xml 文件,它具有调用 java 类的 taskdef 操作。在下面附加的代码中,我在 build.xml 中使用 taskdef 操作调用两个不同的 java 类 HelloWorld.java 和 HelloWorld1.java。
我已经定义了一个单例类以及两个类,并且我正在从上述两个 java 文件中调用这个单例类 ClassicSingleton.java。在尝试打印我从单例类中获得的单例类的实例时,我可以看到为从不同 taskdef 操作调用的 java 类创建了不同的新实例,但我需要对所有 java 类使用相同的单例实例。需要注意的一件事是,当我从同一个 HelloWorld.java 类中两次调用 ClassicSingleton 类中的 getInstance() 方法时,我得到了相同的实例。
如果有任何方法可以将单例类的相同实例用于所有 taskdef 操作,请帮助我。下面附上我的代码。
构建文件
<property name="build.dir" value="${basedir}/build" />
<property name="lib.dir" value="${basedir}/lib" />
<property name="release.dir" value="${basedir}/release"/>
<property name="base.dir" value="E://install/" />
<target name="runclasses" description="Use the Task">
<taskdef name="helloworld" classname="HelloWorld" classpath="HelloWorld.jar"/>
<helloworld/>
<taskdef name="helloworld1" classname="HelloWorld1" classpath="HelloWorld.jar"/>
<helloworld1/>
</target>
<target name="makejar">
<jar destfile="${base.dir}/HelloWorld.jar"
basedir="${build.dir}" includes="**/*.class" />
</target>
<target name="release" depends="makejar,runclasses"/>
</project>
你好世界
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
public class HelloWorld {
public void execute() {
System.out.println("came to Hello World");
ClassicSingleton instance = ClassicSingleton.getInstance();
Map<String,String> mymap = instance.getProperties("E://install/build.properties");
Object value = mymap.get("$AUTH_DB_USER$");
System.out.println(value);
System.out.println(instance);
mymap.put("$AUTH_DB_USER$","sample");
Object val = mymap.get("$AUTH_DB_USER$");
System.out.println(val);
instance = ClassicSingleton.getInstance();
mymap = instance.getProperties("E://install/build.properties");
value = mymap.get("$AUTH_DB_USER$");
System.out.println(value);
System.out.println(instance);
}
}
你好世界1.java
import java.util.Map;
public class HelloWorld1 {
public void execute(){
System.out.println("hai HelloWorld1");
ClassicSingleton instance = ClassicSingleton.getInstance();
Map<String,String> mymap = instance.getProperties("E://install/build.properties");
System.out.println("came to HelloWorld1");
Object value = mymap.get("$AUTH_DB_USER$");
System.out.println(value);
System.out.println(instance);
}
}
经典单例.java
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public synchronized static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
private static final Map<String,String> propertiesMap = new HashMap<String,String>();
public static Map<String,String> getProperties(String propertiesFilePath){
try {
File file = new File(propertiesFilePath);
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();
Set<Object> keys = properties.keySet();
Iterator<Object> iterator = keys.iterator();
while (iterator.hasNext()) {
String string = (String)iterator.next();
StringBuilder key = new StringBuilder("$"+string+"$");
String value = properties.getProperty(string);
propertiesMap.put(key.toString(), value);
}
} catch (Exception exception) {
System.out.println("Exception came");
}
return propertiesMap;
}
}
您必须为两个任务共享类加载器,否则任务将在两个不同且独立的类加载器中加载,因此您将获得两个不同的单例实例(每个类加载器一个)。为此,您可以loaderref
为任务定义提供一个属性(请参阅Typedef任务):
<path id="lib.path">
<fileset dir="${base.dir}" includes="HelloWord.jar"/>
</path>
<target name="runclasses" description="Use the Task">
<taskdef name="helloworld" classname="HelloWorld" classpathref="lib.path" loaderref="lib.path.loader"/>
<helloworld/>
<taskdef name="helloworld1" classname="HelloWorld1" classpathref="lib.path" loaderref="lib.path.loader"/>
<helloworld1/>
</target>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句