在java中调用方法时遇到问题

时间:2013-02-16 21:03:02

标签: java jsf

一个简单的问题,但我不知道,基本上我想以一定的顺序运行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();
    }

1 个答案:

答案 0 :(得分: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()重命名为与其实际工作相匹配的其他内容