如何在执行 ant taskdef 操作的整个过程中拥有一个单例实例 class
How to have one instance of a singleton class throughout the execution of ant taskdef actions
我有一个 build.xml 文件,其中包含调用 java classes 的 taskdef 操作。在下面附带的代码中,我调用了两个不同的 java classes HelloWorld.java 和 HelloWorld1.java 以及 build.xml.
中的 taskdef 操作
我已经定义了一个单例 class 以及两个 class 并且我正在从上面提到的两个 java 中调用这个单例 class ClassicSingleton.java ] 文件。在尝试打印我从单例 class 获得的单例 class 的实例时,我可以看到为从中调用的 java classes 创建了不同的新实例不同的 taskdef 操作,但我需要为所有 java classes 使用相同的单例实例。需要注意的一件事是,当我从同一个 HelloWorld.java class.
中两次调用 ClassicSingleton class 中的 getInstance() 方法时,我得到了相同的实例
如果有任何方法可以对所有 taskdef 操作使用同一个单例 class 实例,请帮助我。
在下面附上我的代码。
build.xml
<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>
HelloWorld.java
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);
}
}
HelloWorld1.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);
}
}
ClassicSingleton.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;
}
}
执行build.xml
时的输出
你必须为你的两个任务共享 class 加载器,否则任务将加载到两个不同且独立的 classloader 中,因此你将获得两个不同的单例实例(一个对于每个 classloader)。为此,您可以为任务定义提供 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>
我有一个 build.xml 文件,其中包含调用 java classes 的 taskdef 操作。在下面附带的代码中,我调用了两个不同的 java classes HelloWorld.java 和 HelloWorld1.java 以及 build.xml.
中的 taskdef 操作我已经定义了一个单例 class 以及两个 class 并且我正在从上面提到的两个 java 中调用这个单例 class ClassicSingleton.java ] 文件。在尝试打印我从单例 class 获得的单例 class 的实例时,我可以看到为从中调用的 java classes 创建了不同的新实例不同的 taskdef 操作,但我需要为所有 java classes 使用相同的单例实例。需要注意的一件事是,当我从同一个 HelloWorld.java class.
中两次调用 ClassicSingleton class 中的 getInstance() 方法时,我得到了相同的实例如果有任何方法可以对所有 taskdef 操作使用同一个单例 class 实例,请帮助我。 在下面附上我的代码。
build.xml
<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>
HelloWorld.java
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);
}
}
HelloWorld1.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);
}
}
ClassicSingleton.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;
}
}
执行build.xml
时的输出
你必须为你的两个任务共享 class 加载器,否则任务将加载到两个不同且独立的 classloader 中,因此你将获得两个不同的单例实例(一个对于每个 classloader)。为此,您可以为任务定义提供 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>