18 Integration with enterprise containers - Reference Documentation
Authors: Andrea Del Bene, Carsten Hufe, Christian Kroemer, Daniel Bartl
Version: 1.0.0.BUILD-SNAPSHOT
Table of Contents
18 Integration with enterprise containers
Writing a web application is not just about producing a good layout and a bunch of “cool” pages. We must also integrate our presentation code with enterprise resources like data sources, message queues, business objects, etc...The first decade of 2000s has seen the rising of new frameworks (like Spring ) and new specifications (like EJB 3.1 ) aimed to simplify the management of enterprise resources and (among other things) their integration with presentation code.All these new technologies are based on the concepts of container and dependency injection. Container is the environment where our enterprise resources are created and configured while dependency injection is a pattern implemented by containers to inject into an object the resources it depends on.Wicket can be easily integrated with enterprise containers using component instantiation listeners. These entities are instances of interface org.apache.wicket.application.IComponent InstantiationListener and can be registered during application's initialization. IComponentInstantiationListener defines callback method onInstantiation(Component component) which can be used to provide custom instantiation logic for Wicket components.Wicket distribution and project WicketStuff already provide a set of built-in listeners to integrate our applications with EJB 3.1 compliant containers (like JBoss Seam) or with some of the most popular enterprise frameworks like Guice or Spring.In this chapter we will see two basic examples of injecting a container-defined object into a page using first an implementation of the EJB 3.1 specifications (project OpenEJB ) and then using Spring.18.1 Integrating Wicket with EJB
WicketStuff provides a module called wicketstuff-javaee-inject that contains component instantiation listener JavaEEComponentInjector. If we register this listener in our application we can use standard EJB annotations to inject dependencies into our Wicket components.To register a component instantiation listener in Wicket we must use Application's method getComponentInstantiationListeners which returns a typed collection of IComponent InstantiationListeners.The following initialization code is taken from project EjbInjectionExample:public class WicketApplication extends WebApplication { //Constructor... @Override public void init() { super.init(); getComponentInstantiationListeners().add(new JavaEEComponentInjector(this)); } }
@ManagedBean public class EnterpriseMessage { public String message = "Welcome to the EJB world!"; }
ManagedBean to decorate our object. Now to inject it into the home page we must add a field of type EnterpriseMessage and annotate it with annotation
EJB:public class HomePage extends WebPage { @EJB private EnterpriseMessage enterpriseMessage; //getter and setter for enterpriseMessage... public HomePage(final PageParameters parameters) { super(parameters); add(new Label("message", enterpriseMessage.message)); } }
18.2 Integrating Wicket with Spring
If we need to inject dependencies with Spring we can use listener org.apache.wicket.spring. injection.annot.SpringComponentInjector provided by module wicket-spring.For the sake of simplicity in the example project SpringInjectionExample we have used Spring class AnnotationConfigApplicationContext to avoid any XML file and create a Spring context directly from code:public class WicketApplication extends WebApplication { //Constructor... @Override public void init() { super.init(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); //Scan package for annotated beans ctx.scan("org.wicketTutorial.ejbBean"); ctx.refresh(); getComponentInstantiationListeners().add(new SpringComponentInjector(this, ctx)); } }
@ManagedBean public class EnterpriseMessage { public String message = "Welcome to the Spring world!"; }
public class HomePage extends WebPage { @SpringBean private EnterpriseMessage enterpriseMessage; //getter and setter for enterpriseMessage... public HomePage(final PageParameters parameters) { super(parameters); add(new Label("message", enterpriseMessage.message)); } }
//set the dependency as not required, i.e the field can be left null @SpringBean(name="anotherName", required=false) private EnterpriseMessage enterpriseMessage;
18.3 JSR-330 annotations
Spring (and Guice) users can use standard JSR-330 annotations to wire their dependencies. This will make their code more interoperable with other containers that support this standard://inject a bean specifying its name with JSR-330 annotations
Inject
Named("anotherName")
private EnterpriseMessage enterpriseMessage;