如何在ant taskdef动作的执行过程中拥有一个单例类的实例

时间:2017-04-04 06:49:48

标签: java xml ant singleton taskdef

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

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

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

的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时输出 Output when i executed the build.xml

1 个答案:

答案 0 :(得分: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>