-1

一个简单的问题,但我几乎不记得了,基本上我想按特定顺序运行 java 方法,我确实让它工作得很好,但我不得不在开始时添加一些东西,现在它不会按顺序运行

基本上之前是这段代码,

@PostConstruct
    public void init() {


        //System.out.println(destinationPDF);
        //System.out.println(destination);

// Get the username from the login page, this is used to create a folder for each user
        System.out.println("called get username");
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }

    public void File() {
        File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution)
        theFile.mkdirs();
        System.out.println("Completed File");
    }

它会运行并自动调用下一个需要的方法,它会按以下顺序调用它们:

INFO: buttonToUploadText invoked
INFO: called get username
INFO: called handle file
INFO: Completed Creation of folder
INFO: Now in copying of file proccess
INFO: Completed Creation of folder for copy of PDF
INFO: End of copying file creation
INFO: Called CopyFile
INFO: New file created!
INFO: Copying is now happening

但是我添加了一个新方法,它从文件中调用变量:

@PostConstruct
    public void loadProp() {
        System.out.println("Loading properties");
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties"); //points to a properties file, this will load up destinations instead of having to declare them here
        try {
            configProp.load(in);
            System.out.println(configProp.getProperty("destinationPDF"));
            System.out.println(configProp.getProperty("destination"));
            System.out.println(configProp.getProperty("fileList"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

现在它必须在被触发时首先运行才能声明变量,但是现在它现在将在完成后运行 public void int() 而不是跳过很多并运行 public void handleFileUpload

那么从 public void loadProp() { 调用 public void init() 的最佳方法是什么?

编辑2:

    private Properties configProp = new Properties();

    public void loadProp() {
        System.out.println("Loading properties");
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties"); //points to a properties file, this will load up destinations instead of having to declare them here
        try {
            configProp.load(in);
            System.out.println(configProp.getProperty("destinationPDF"));
            System.out.println(configProp.getProperty("destination"));
            System.out.println(configProp.getProperty("fileList"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private String destinationPDF = configProp.getProperty("destinationPDF");
    public String destination = configProp.getProperty("destination");
    private String username;
    //public static String destination = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/uploaded/"; // main location for uploads//TORNADO ONLY //"D:/My Documents/NetBeansProjects/printing~subversion/fileupload/uploaded/"; // USE ON PREDATOR ONLY 
    public static String NewDestination;
    public static String UploadedfileName;
    public static String CompletefileName;
    //
    //Strings for file copy
    //
    //private String destinationPDF = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/"; //USE ON TORNADO//"D:/My Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/";//USE ON PREDATOR    
    private String NewdestinationPDF;
    public static String PdfLocationViewable;

    //
    @PostConstruct
    public void init() {

       FileUploadController.loadProp();

        //System.out.println(destinationPDF);
        //System.out.println(destination);

// Get the username from the login page, this is used to create a folder for each user
        System.out.println("called get username");
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
4

1 回答 1

1

您可以而且应该只有一种@PostConstruct方法。

代替

@PostConstruct
public void loadProp() {
    // ...
}

@PostConstruct
public void init() {
    // ...
}

经过

@PostConstruct
public void postConstruct() {
    loadProp();
    init();
}

private void loadProp() {
    // ...
}

private void init() {
    // ...
}

(我只考虑重命名并将原件重命名为postConstruct()与其实际工作相匹配的其他名称)init()init()

于 2013-02-17T02:57:22.633 回答