下面是一个完整的EJB3 session bean的 HelloWorld 例子,环境:Eclipse +JBoss
1.Download
i downloaded jboss 4.2.2.GA and extracted it here:
C:\Program Files\jboss
2. start eclipse
3. install jbosstools
help > software updates > find and install > search for new features to install > new remote site:
name: jboss
url: http://download.jboss.org/jbosside/updates/stable
put a check mark next to "jboss" > Finish
install all the jboss stuff
restart eclipse
4. create a new ejb3 project
file > new > Project (the white icon , NOT the purple one) > EJB > EJB Project
next
Project name: MyFirstEJBProject
Target runtime: JBoss 4.2 runtime
Configurations: Default
finish
5. create EJB3 session bean
highlight MyFirstEJBProject in the project explorer
(rightclick) > new > other > EJB > EJB3 Session Bean
next
bean package: myejb3test
bean name: EJB3HelloWorld
finish
6. configure the project environment
highlight MyFirstEJBProject in the project explorer
(rightclick) > properties:
java build path > libraries > jre system library:
make sure this is an updated version of the java jre (i think you need java 5+, i am using jre1.6.0_6)
server > choose JBoss 4.2 server > apply
If it is not in the list of servers then just set up a new server:
close that properties window
file > new > other > server > server
next
JBoss, a division of Red Hat > JBoss AS, 4.2
next
available projects: highlight MyFirstEJBProject and then push the Add button to add it to Configured Projects
finish
7. write your bean code
expand the ejbModule folder > expand the myejb3test folder:
open EJB3HelloWorld.java
paste in this code:
package myejb3test; import javax.ejb.Remote; @Remote public interface EJB3HelloWorld { public void sayHello(String text); }
open EJB3HelloWorldBean.java
paste in this code:
package myejb3test; import javax.ejb.Stateless; import myejb3test.EJB3HelloWorld; public @Stateless class EJB3HelloWorldBean implements EJB3HelloWorld { public void sayHello(String hello) { System.out.println(hello); } }
right click myejb3test in the project explorer > new > class
name: EJB3HelloWorldClient
put a check mark by public static void main(String[] args)
finish
open EJB3HelloWorldClient.java
paste in this code:
package myejb3test; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class EJB3HelloWorldClient { public static void main(String[] args) { Properties props = new Properties(); props.put("java.naming.factory.initial", "org.jboss.naming.HttpNamingContextFactory" ); props.put("java.naming.provider.url", "http://localhost:8080/invoker/JNDIFactory"); props.put("java.naming.factory.url.pkgs", "org.jboss.naming"); try { Context context = new InitialContext(props); System.out.println("Looking up HelloWorld"); try { EJB3HelloWorld helloWorld = (EJB3HelloWorld)context.lookup("EJB3HelloWorldBean/remote"); helloWorld.sayHello("Hello world"); } catch (NamingException e) { System.out.println("couldnt look it up"); e.printStackTrace(); } } catch (NamingException e) { System.out.println("naming exception"); e.printStackTrace(); } } }
8. start the jboss server
in the Servers tab at the bottom of eclipse, right click the JBoss 4.2 Server > start
wait for it to start up
9. run your code =)
right click somewhere in the EJB3HelloWorldClient.java file > run as > java application