How to define Locale scope?
I want to create different Bean instances for different Locales in my program, but I don't know too much about the scoping mechanism in Spring CDI. How does scope-proxy determine which proxy-target to forward, if there are two request-scoped instances of the same bean is simulaneously in used?
I can get the Locale preference from an http request, and then I want to get the correct bean in that specific locale. Rather then using "prototype" scope, Locale-scope will create only a few instances for locales in used only. Personally, I want something like this in my ow开发者_如何学Gon way:
@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() {
// ...
}
});
}
}
Well, it's obviously not work.
Any idea?
See related wiki page for a LocaleBeanScope implementation
精彩评论