1

我有一个 build.xml 文件,其中包含调用 java 类的 taskdef 操作。在下面附加的代码中,我使用 build.xml 中的 taskdef 操作调用两个不同的 java 类 HelloWorld.java 和 HelloWorld1.java。

我已经定义了一个单例类以及这两个类,并且我正在从上述两个 java 文件中调用这个单例类 ClassicSingleton.java。在尝试打印从单例类中获取的单例类的实例时,我可以看到为从不同 taskdef 操作调用的 Java 类创建了不同的新实例,但我需要为所有 Java 类使用相同的单例实例。需要注意的一件事是,当我从同一个 HelloWorld.java 类两次调用 ClassicSingleton 类中的 getInstance() 方法时,我得到了同一个实例。

如果有任何方法可以将单例类的相同实例用于所有 taskdef 操作,请帮助我。在下面附上我的代码。

构建.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 时的输出
执行 build.xml 时的输出

4

1 回答 1

1

您必须为两个任务共享类加载器,否则任务将加载到两个不同且独立的类加载器中,因此您将获得两个不同的单例实例(每个类加载器一个)。为此,您可以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>
于 2017-04-05T13:57:49.153 回答