Wicket依赖注入 [英] Wicket Dependency Injection
问题描述
我在Wicket中有一个带有表单的页面,其中表单需要协作者完成其工作。合作者注入(我正在使用Guice),看起来像:
I've got a page with a form in Wicket where the form requires a collaborator to get its job done. The collaborator is injected (for which I'm using Guice) and looks something like:
public class RegistrationPage extends WebPage {
@Inject
public RegistrationPage(RegistrationService service) {
this.service = service;
add(new RegistrationForm());
}
private class RegistrationForm extends Form {
public RegistrationForm() {
// setup
}
protected void onSubmit() {
service.doSomething();
}
}
}
我不喜欢想法只是在需要它的RegistrationForm时将RegistrationService注入到RegistrationPage中。我可以更改RegistrationForm以接收RegistrationService:
I don't like the idea that the RegistrationService is injected into the RegistrationPage when it's just the RegistrationForm that needs it. I could change the RegistrationForm to receive the RegistrationService:
public RegistrationForm(RegistrationService service) {
this.service = service;
}
并从RegistrationPage中删除该字段,但RegistrationPage仍在使用做传递。
and remove the field from the RegistrationPage, but the RegistrationPage is still being used to do the pass-through.
我想我问的是这样做的最佳做法是什么?这样做是否可行,或者将RegistrationForm注入页面可能更好:
I guess what I'm asking is what the best-practise is for doing this? Is this ok to do, or would it perhaps be better to inject the RegistrationForm itself into the Page:
public class RegistrationPage extends WebPage {
@Inject
public RegistrationPage(RegistrationForm form) {
add(form);
}
}
---
private class RegistrationForm extends Form {
private RegistrationService service;
@Inject
public RegistrationForm(RegistrationService service) {
this.service = service;
}
protected void onSubmit() {
service.doSomething();
}
}
我更喜欢这个,因为我想将RegistrationForm放在一个单独的类/文件中。我对Wicket很陌生,所以不确定规范是什么 - 有人能告诉我引导光吗? :)
I'd prefer this as I'd like to have the RegistrationForm in a separate class/file. I'm quite new to Wicket so unsure of what the norm is - can someone show me the guiding light? :)
推荐答案
wicket + ioc的基本范例是:大多数依赖项应该通过setter注入注入。 WebPages是不可能构造函数注入的。
the basic paradigm with wicket+ioc is: most dependencies should be injected via setter injection. constructor injection is impossible for WebPages.
组件/面板/表单/页面应仅在接收端。
components/panels/forms/pages should only be on the recieving end.
所以,将依赖项 RegistrationService
快乐地注入 RegistrationForm
,然后在RegistrationPage中使用add(new)创建它RegistrationForm());
so, inject the dependency to RegistrationService
happily into the RegistrationForm
, then create it in the RegistrationPage with add(new RegistrationForm());
wicket有IComponentInstantiationListener - 其中一个是guice。他们会在每个组件/网页的构造函数中得到通知。因此,您的RegistrationForm将在您的代码的任何部分可以执行之前注入其依赖项。
wicket has IComponentInstantiationListener - one of them is guice. they get notified during the constructor of each component/webpage. so your RegistrationForm will have its dependencies injected before any part of your code can execute.
我的方式:
(当然RegistrationForm可以在另一个文件)
the way i would do it: (of course RegistrationForm can be in another file)
public class RegistrationPage extends WebPage {
@Inject
public RegistrationPage() {
add(new RegistrationForm());
}
---
private static class RegistrationForm extends Form {
RegistrationService service;
@Inject
public void setRegistrationService (RegistrationService service){
this.service = service;
}
public RegistrationForm() {
// setup
}
protected void onSubmit() {
service.doSomething();
}
}
}
如果您决定放入RegisterForm里面的Page作为内部类,记得把它声明为静态!你很可能不需要任何对封闭类的引用。
if you decide to put the RegistrationForm inside the Page as inner class, remember to declare it static! you will most likely not need any references to the enclosing class.
这篇关于Wicket依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!