先前是看了传智博客的EJB视频,然后又看了下《精通EJB3》都是用的注释方式,我本人不喜欢注释方式,所以就花了部分时间去查看了下有关注释方面的内容,最后终于将注释的改成了ejb-jar.xml配置文件方式。所做的大概有 有状态bean、无状态bean、会话bean、消息驱动bean、简单JPA操作几个方面。
环境:Jboss 5.1 Mysql 5.1 jdk1.6
今天就把我的第一个无状态bean先写下来,由于上班没时间。所以在附件里长传全部代码;
1、创建项目,建立好各个目录后,将JBOSS中的client里面jar引入进项目,配置好Jboss数据源
2、编写ant的build.xml文件,
<?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== 2010-7-17 下午02:10:44 HelloBean description lyjBack ====================================================================== --> <project name="EJB001" default="deploy" basedir="."> <property name="src.dir" value="${basedir}\src" /> <property environment="env" /> <property name="jboss.home" value="${env.JBOSS_HOME}" /> <property name="jboss.server.config" value="default" /> <property name="build.dir" value="${basedir}\build" /> <path id="build.classpath"> <fileset dir="${jboss.home}\client"> <include name="*.jar" /> </fileset> <pathelement path="${build.dir}" /> </path> <target name="prepare"> <delete dir="${build.dir}" /> <mkdir dir="${build.dir}" /> </target> <target name="compile" depends="prepare" description="编译"> <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath"> </javac> </target> <target name="ejbjar" depends="compile" description="创建ejb包"> <jar jarfile="${basedir}\${ant.project.name}.jar"> <fileset dir="${build.dir}"> <include name="**/*.class" /> </fileset> <metainf dir="${src.dir}\META-INF"> </metainf> </jar> </target> <target name="deploy" depends="ejbjar" description="发布"> <copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" /> </target> <target name="undeploy" description="卸载ejb"> <delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar" /> </target> </project>
这里要注意的是:
1>build.xml中用到环境变量的一定在环境变量里面已经配置。
2>eclipse配置中,默认有集中提醒没选中,所以,得到eclipse配置ant配置中把problems的部分勾上。
3、编写bean接口
/**
*
*/
package com.lyj.ejb.stateless;
/**
* @author lyjBack
*
*/
public interface Hello {
public String hello(String name);
}
编写实现远程接口的bean
/**
*
*/
package com.lyj.ejb.stateless.impl;
import com.lyj.ejb.stateless.Hello;
//import javax.ejb.Local;
//import javax.ejb.Remote;
//import javax.ejb.Stateless;
/**
* @author lyjBack
*
*/
//@Stateless
//@Remote(Hello.class)
public class HelloBean implements Hello{
/* (non-Javadoc)
* @see com.lyj.ejb.stateless.Hello#hello()
*/
public String hello(String name) {
return name+"说:hello world";
}
}
其中注释部分我没删除,只是注释起来。
4、写ejb-jar.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">
<enterprise-beans>
<!-- CountBean -->
<session>
<ejb-name>CountBean</ejb-name>
<business-remote>com.lyj.ejb.stateful.Count</business-remote>
<ejb-class>com.lyj.ejb.stateful.impl.CountBean</ejb-class>
<session-type>Stateful</session-type>
<remove-method>
<bean-method>
<method-name>remove</method-name>
</bean-method>
</remove-method>
<transaction-type>Container</transaction-type>
</session>
<!-- HelloBean -->
<session>
<ejb-name>HelloBean</ejb-name>
<business-local>com.lyj.ejb.stateless.HelloLocal</business-local>
<business-remote>com.lyj.ejb.stateless.Hello</business-remote>
<ejb-class>com.lyj.ejb.stateless.impl.HelloBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<!-- UserServiceBean -->
<session>
<ejb-name>UserServiceBean</ejb-name>
<business-remote>com.lyj.ejb.service.UserService</business-remote>
<ejb-class>com.lyj.ejb.service.impl.UserServiceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<message-driven>
<ejb-name>LogBean</ejb-name>
<ejb-class>com.lyj.ejb.jms.queue.LogBean</ejb-class>
<transaction-type>Container</transaction-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>queue/lyjQueue</activation-config-property-value>
</activation-config-property>
</activation-config>
<pre-destroy>
<lifecycle-callback-method>remove</lifecycle-callback-method>
</pre-destroy>
</message-driven>
<message-driven>
<ejb-name>LogBeanFirst</ejb-name>
<ejb-class>com.lyj.ejb.jms.topic.LogBeanFirst</ejb-class>
<transaction-type>Container</transaction-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Topic</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>topic/lyjTopic</activation-config-property-value>
</activation-config-property>
</activation-config>
<pre-destroy>
<lifecycle-callback-method>remove</lifecycle-callback-method>
</pre-destroy>
</message-driven>
<message-driven>
<ejb-name>LogBeanSecond</ejb-name>
<ejb-class>com.lyj.ejb.jms.topic.LogBeanSecond</ejb-class>
<transaction-type>Container</transaction-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Topic</activation-config-property-value>
</activation-config-property>
<activation-config-property>
<activation-config-property-name>destination</activation-config-property-name>
<activation-config-property-value>topic/lyjTopic</activation-config-property-value>
</activation-config-property>
</activation-config>
<pre-destroy>
<lifecycle-callback-method>remove</lifecycle-callback-method>
</pre-destroy>
</message-driven>
</enterprise-beans>
<interceptors>
<description></description>
<interceptor>
<interceptor-class>com.lyj.ejb.stateful.CountCallbacks</interceptor-class>
<post-construct>
<lifecycle-callback-method>construct</lifecycle-callback-method>
</post-construct>
<pre-destroy>
<lifecycle-callback-method>destroy</lifecycle-callback-method>
</pre-destroy>
<post-activate>
<lifecycle-callback-method>activate</lifecycle-callback-method>
</post-activate>
<pre-passivate>
<lifecycle-callback-method>passivate</lifecycle-callback-method>
</pre-passivate>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>CountBean</ejb-name>
<interceptor-class>com.lyj.ejb.stateful.CountCallbacks</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
由于所有的配置都在这
5、编写EJB客户测试类
/**
*
*/
package com.lyj.ejb.client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.lyj.ejb.stateless.Hello;
/**
* @author lyjBack
*
*/
public class EJBClient {
/**
* @param args
* @throws NamingException
*/
public static void main(String[] args) throws NamingException {
Context ctx = new InitialContext();
Hello hello = (Hello) ctx.lookup("HelloBean/remote");
System.out.println(hello.hello("JAVA EE"));
}
}
6、编写EJB容器的jndi信息,如果是用Jboss5.1,那么就这样写
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099
7、然后基本就完了,运行build.xml,然后启动Jboss。应该就能看到效果。
如果不想做第四步。那么就将代码中注释元数据的注释删除,应该就也能运行