Using Spring Beans in Wicket

Spring and Wicket work great together, and using Spring Beans in Wicket is just one small example. Here is how easy it is to use a Spring Bean in Wicket.

We begin in the WicketApplication class which serves as Spring’s starting point by using Spring Boot’s @EnableAutoConfiguration annotation and providing a main() method. In the WicketApplication init() method we’re making wicket pages injectable by activating the SpringComponentInjector with Spring’s ApplicationContext.

We create our Spring Context using Spring Annotations in a class named SpringConfiguration.

Now we can move to our (very simple) TextService class, annotated with @Service. We’ll be returning “delivered by spring service” to our Wicket page next.

In our Wicket page class we instantiate our TextService with @SpringBean (Wicket’s @AutoWired equivalent) and display getText() in our Wicket label.

Which displays on the page as you see, “delivered by spring service…”

Source Code Notes for this Post

Topics discussed in this post can be found in the v0.0.1 Branch of my Spring-Wicket-Bootstrap GitHub repo and viewed online here.