2012-02-28 74 views
0

我的英文不好。請理解..彈簧動態構造函數值

//This is my Spring-mvc-based Controller 
@Controller 

public class DownloadManageController { 
    private DownloadManageService downService; 

    @Autowired 
    public void setPackService(DownloadManageService downService) { 
     this.downService = downService; 
    } 
} 

//This is my Spring-Bean which should have a dynamic value of contructor 
Component 

public class DownloadManageService { 
    private Log log = LogFactory.getLog(DownloadManageService.class); 
    private FileAccessObject downloadFileAccessObj = null; 

    DownloadManageService(String downloadInfoFile) { 
     this.downloadFileAccessObj = new FileAccessObjectImpl(
       "d:/dat/download/" + downloadInfoFile); 
    } 

} 

當服務注入到控制器......我想打downloadInfoFile變量的動態取決於請求參數。

回答

1

我建議你創建一個工廠類,你可以在其中傳遞你想要的路徑。

0

如果是我,我會做如下:

public class DownloadManageService { 
    private Log log = LogFactory.getLog(DownloadManageService.class); 
    private FileAccessObject downloadFileAccessObj = null; 

    public DownloadManageService() { 
    } 

    public void setDownloadFileAccessObj... 
} 

public class DownloadManageController { 
    private DownloadManageService downService; 

    @Autowired 
    public void setPackService(DownloadManageService downService) { 
     this.downService = downService; 
    } 

    public void exec(){ 
     ... 
     this.downService.setDownloadFileAccessObj(new FileAccessObjectImpl(...)) 
     ... 
    } 
} 
+0

謝謝你的回答~~好幫手 – jeon 2012-03-09 16:31:01