Wicket Dependency Injection

岁酱吖の 提交于 2019-12-04 10:24:23

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.

so, inject the dependency to RegistrationService happily into the RegistrationForm , then create it in the RegistrationPage with add(new 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.

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();
    }
}
}

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.

In my opinion DI on Wicket has one confusing element, is done automagically on Components or Pages but not in Models. In my opinion exactly Model (but not page) should have dependency to JPA etc.

Official doc say to use InjectorHolder.getInjector().inject(this)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!