...
Developing a webapp using EJB3
The JEE tutorial from Sun will take you through how to create EJB3 classes.
Here's an extremely simple example of a stateless session bean showing some of the JEE-style annotations:
| Code Block | ||
|---|---|---|
| ||
package com.acme;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
@Stateless
@Remote(Echo.class)
public class EchoBean implements Echo
{
public String echo()
{
return "Hello "+new Date();
}
@PostConstruct
public void init()
{
System.err.println("EchoBean init called");
}
}
|
...
In order to have Pitchfork notice that the com.acme.EchoTest class wants a reference to reference the com.acme.EchoBean ejb, we declare it in the Spring WEB-INF/applicationContext.xml file:
| Code Block | ||
|---|---|---|
| ||
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="no"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="echoTest" class="com.acme.EchoTest"/>
</bean>
<bean id="jeeEjbPostProcessor" class="org.springframework.jee.ejb.config.JeeEjbBeanFactoryPostProcessor"/>
</beans>
|
Notice the very important second line with id jeeEjbPostProcessor. This line tells Pitchfork to look at all other <bean> declarations and perform any necessary resource injections on them.
To complete the webapp, here's a trivial jsp:
| Code Block |
|---|
<html><head>
<%@ page import="com.acme.*" %>
</head><body>
<h1>Echo EJB</h1>
<%
String str = EchoTest.echo();
%>
<%=str%>
</body></html>
|
When you deploy it and surf to it, you'll see something like this:
| Panel | ||||||||
|---|---|---|---|---|---|---|---|---|
| ||||||||
|
A maven project that builds a deployble webapp for example we've been looking at is attached to this page.