2011-03-27 118 views
0

我想在我的程序中爲不同的區域設置創建不同的Bean實例,但我不太瞭解Spring CDI中的作用域機制。如果同一個bean的兩個請求範圍實例同時使用,範圍代理如何確定要轉發哪個代理目標?如何定義區域設置範圍?

我可以從http請求中獲取Locale首選項,然後我想要在該特定區域設置中獲得正確的bean。而不是使用「原型」範圍,區域設置範圍將只爲僅使用的語言環境創建幾個實例。就個人而言,我想在我自己的方式是這樣的:

@Component 
@Scope("locale") 
class MyService { 

    @Inject 
    @Named("scope-invariant") 
    public MyService(Locale locale) { 
     ResourceBundle nls = getResourceBundle(..., locale); 
     // ... 
    } 

} 

@Controller 
class MyController { 

    void service(HttpServletRequest req, HttpServletResponse resp) { 
     UserPreference userPreference = getUserPreference(req, res.getSession(), ...); 
     Locale userLocale = userPreference.getUserLocale(); 

     applicationContext.doInScope(
      new ScopeBinding("locale", userLocale), 
      new ScopedCallback() { 

       @Inject 
       MyService service; 

       void execute() { 
        // ... 
       } 

      }); 
    } 

} 

嗯,這顯然是不行的。

有什麼想法?

回答