2013-07-10 124 views
0

動態參數值的方式自動裝配類我想做的事情在我的Spring MVC應用程序如下:在春季

  1. 從數據庫中獲取一些數據(它會返回List<String> list)在我的@Service MainService
  2. 使用此list因爲我已經在MainService目前@Autowired其他@Service ConfigFactory構造函數的參數。
  3. 火從ConfigFactory的方法來獲取將被添加到的ModelAndView在我@Controller類最終結果。

我知道這將有可能這樣那樣:

ConfigFactory類:

@Service 
public class ConfigFactory(){ 
    public void init(List<String> list){ 
     //Use list to initialize ConfigFactory 
    } 

    public Result getResult(){ 
     //Do some business logic 
     return result; 
    } 
} 

MainService類:

@Service 
public class MainService { 
    @Autowired ConfigFactory configFactory; 

    public Result method(){ 
     //Get list from database; 
     configFactory.init(list); 
     Result result = configFactory.getResult(); 

     //Create a Result that will be later added to controller ModelAndView. 
    } 
} 

但給人的感覺並不很好。所以我被困在這裏。有沒有人有一個想法如何正確認識到這一點?

回答

1

你爲什麼不通過列表作爲參數傳遞給getResult()方法? 我想你在init()方法從列表中獲取價值,而你在初始化你ConfigFactory一些屬性?不要這樣做,當幾個用戶試圖做同樣的事情時,你可能會遇到問題。

+0

是。我根據其中不僅僅是設置此列表的更多'的init()'方法進行邏輯得到的值。如果必須稍後使用任何其他方法,則傳遞列表將會有問題,例如, 'getOtherResult()',因爲它需要包含這個初始化邏輯。但我認爲我會這樣做,並不會在那裏創造更多的公共方法。感謝您的評論。 – Disper