有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在整个ant taskdef操作的执行过程中拥有一个singleton类实例

我有身材。包含taskdef操作以调用java类的xml文件。在下面的代码中,我调用了两个不同的java类HelloWorld。java和HelloWorld1。java在构建中使用taskdef操作。xml

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

如果有任何方法可以将singleton类的同一实例用于所有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>

地狱世界。爪哇

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。爪哇

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);
    }
}

古典林格顿。爪哇

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;
    }
}

执行构建时的输出。xml
Output when i executed the build.xml


共 (1) 个答案

  1. # 1 楼答案

    您必须为两个任务共享类加载器,否则任务将被加载到两个不同的独立类加载器中,因此您将得到两个不同的单例实例(每个类加载器一个)。要实现这一点,可以为任务定义提供loaderref属性(请参见Typedeftask):

    <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>