GitHub

User guideIntegration

Inject with JSR-330 annotations

Use the standard @Inject and @Named annotations instead of @SpringBean, with the Spring, Guice and CDI integrations.

How-toWicket 10 and later

Use this when you want components that do not depend on one container’s annotations. The Jakarta Dependency Injection annotations (jakarta.inject.Inject, jakarta.inject.Named, formerly JSR-330) work with every injection module of Wicket. They come with wicket-ioc, which wicket-spring and wicket-guice depend on, so you need no extra dependency.

Only the jakarta.inject package works. Fields annotated with the old javax.inject.Inject are not injected.

With Spring

Register SpringComponentInjector as described in Inject Spring beans. It injects fields annotated with @Inject as well as @SpringBean:

public class ProductPage extends WebPage {

    @Inject
    private ProductService products;

    @Inject
    @Named("archiveProductService")
    private ProductService archive;
}

@Named selects a bean by name, like @SpringBean(name = ...). A field with @Inject is always required: when no bean matches, Wicket throws an IllegalStateException. For an optional dependency, use @SpringBean(required = false).

With Guice

Module wicket-guice injects fields annotated with @jakarta.inject.Inject or @com.google.inject.Inject. Register the injector with your Guice modules in init():

@Override
protected void init() {
    super.init();
    getComponentInstantiationListeners().add(new GuiceComponentInjector(this, new ServicesModule()));
}

A qualifier on the field, such as @Named("archive") or your own annotation marked @jakarta.inject.Qualifier or @BindingAnnotation, selects the binding. Only one qualifier per field is allowed. Optional injection needs Guice’s own annotation, @com.google.inject.Inject(optional = true).

Like the Spring integration, wicket-guice injects serializable proxies, so the same rules apply: inject interfaces or non-final classes, and add Objenesis for classes without a constructor without arguments. See Keep injected fields serializable.

With CDI

@Inject is CDI’s own annotation, so nothing changes; see Inject CDI beans. Qualifiers work as defined by CDI.

Inject other objects

With Spring and Guice, objects that Wicket does not create are injected by calling Injector.get().inject(this) in their constructor, whichever annotation they use. With CDI, use NonContextual.of(this).inject(this).