在开发ejb之前,我们先得配置好服务器,我使用的是Weblogic9.0中文版,关于Weblogic9.0配置请看我的另一片文章。
配置Weblogic9.0
首先需要配置好eclipse。我这里主要说明weblogic的配置。
注意JDK选择JDK5.0的版本。
顺便将weblogic8的配置也贴上来,供大家比较参考
注意weblogic8的JDK版本必须是JDK1.4。
接下来就开始我们的开发了。
下面就是SessionBean的代码
30
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 package com.ejb;
2
3 import java.rmi.RemoteException;
4
5 import javax.ejb.EJBException;
6 import javax.ejb.SessionBean;
7 import javax.ejb.SessionContext;
8
9 /**
10 * XDoclet-based session bean. The class must be declared
11 * public according to the EJB specification.
12 *
13 * To generate the EJB related files to this EJB:
14 * - Add Standard EJB module to XDoclet project properties
15 * - Customize XDoclet configuration for your appserver
16 * - Run XDoclet
17 *
18 * Below are the xdoclet-related tags needed for this EJB.
19 *
20 * @ejb.bean name="HelloWorld"
21 * display-name="Name for HelloWorld"
22 * description="Description for HelloWorld"
23 * jndi-name="ejb/HelloWorld"
24 * type="Stateless"
25 * view-type="remote"
26 */
27 public class HelloWorld implements SessionBean {
28
29 /** The session context */
30 private SessionContext context;
31
32 public HelloWorld() {
33 super();
34 // TODO 自动生成构造函数存根
35 }
36
37 /**
38 * Set the associated session context. The container calls this method
39 * after the instance creation.
40 *
41 * The enterprise bean instance should store the reference to the context
42 * object in an instance variable.
43 *
44 * This method is called with no transaction context.
45 *
46 * @throws EJBException Thrown if method fails due to system-level error.
47 */
48 public void setSessionContext(SessionContext newContext)
49 throws EJBException {
50 context = newContext;
51 }
52
53 public void ejbRemove() throws EJBException, RemoteException {
54 // TODO 自动生成方法存根
55
56 }
57
58 public void ejbActivate() throws EJBException, RemoteException {
59 // TODO 自动生成方法存根
60
61 }
62
63 public void ejbPassivate() throws EJBException, RemoteException {
64 // TODO 自动生成方法存根
65
66 }
67
68 /**
69 * An example business method
70 *
71 * @ejb.interface-method view-type = "remote"
72 *
73 * @throws EJBException Thrown if method fails due to system-level error.
74 */
75 public String hello() throws EJBException {
76 // rename and start putting your business logic here
77 return new String("HelloEJBWorld!");
78 }
79
80 }
81
其实就是修改了其中的一个方法:
配置属性
添加weblogic.jar。我的路径是:bea\weblogic90\server\lib\weblogic.jar
就下来写EJBTest类:
最后就是看结果了,先启动weblogic,然后运行EJBTest程序。
首先需要配置好eclipse。我这里主要说明weblogic的配置。
![ejb19.jpg](https://i-blog.csdnimg.cn/blog_migrate/5d79f9ce661a462f268a3c11c37e5ee0.jpeg)
注意JDK选择JDK5.0的版本。
顺便将weblogic8的配置也贴上来,供大家比较参考
![ejb18.jpg](https://i-blog.csdnimg.cn/blog_migrate/c503e5ddc125fa3a94c8d60f97b8aa13.jpeg)
注意weblogic8的JDK版本必须是JDK1.4。
接下来就开始我们的开发了。
![ejb1.jpg](https://i-blog.csdnimg.cn/blog_migrate/ffdf445d06b476ada43a6e52c5d895e9.jpeg)
![ejb2.jpg](https://i-blog.csdnimg.cn/blog_migrate/7982dcb8765d0bb20d5d0f242ee7eb53.jpeg)
![ejb3.jpg](https://i-blog.csdnimg.cn/blog_migrate/c0d8c7a2762c02803151ce0b33d12774.jpeg)
![ejb4.jpg](https://i-blog.csdnimg.cn/blog_migrate/26d633e698ee0994d074c8922e0f9570.jpeg)
![ejb5.jpg](https://i-blog.csdnimg.cn/blog_migrate/b0ec82498192018b3a6648fedd90c15a.jpeg)
下面就是SessionBean的代码
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/ca75c07623e1b494fee67e8f316fc310.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 1 package com.ejb;
2
3 import java.rmi.RemoteException;
4
5 import javax.ejb.EJBException;
6 import javax.ejb.SessionBean;
7 import javax.ejb.SessionContext;
8
9 /**
10 * XDoclet-based session bean. The class must be declared
11 * public according to the EJB specification.
12 *
13 * To generate the EJB related files to this EJB:
14 * - Add Standard EJB module to XDoclet project properties
15 * - Customize XDoclet configuration for your appserver
16 * - Run XDoclet
17 *
18 * Below are the xdoclet-related tags needed for this EJB.
19 *
20 * @ejb.bean name="HelloWorld"
21 * display-name="Name for HelloWorld"
22 * description="Description for HelloWorld"
23 * jndi-name="ejb/HelloWorld"
24 * type="Stateless"
25 * view-type="remote"
26 */
27 public class HelloWorld implements SessionBean {
28
29 /** The session context */
30 private SessionContext context;
31
32 public HelloWorld() {
33 super();
34 // TODO 自动生成构造函数存根
35 }
36
37 /**
38 * Set the associated session context. The container calls this method
39 * after the instance creation.
40 *
41 * The enterprise bean instance should store the reference to the context
42 * object in an instance variable.
43 *
44 * This method is called with no transaction context.
45 *
46 * @throws EJBException Thrown if method fails due to system-level error.
47 */
48 public void setSessionContext(SessionContext newContext)
49 throws EJBException {
50 context = newContext;
51 }
52
53 public void ejbRemove() throws EJBException, RemoteException {
54 // TODO 自动生成方法存根
55
56 }
57
58 public void ejbActivate() throws EJBException, RemoteException {
59 // TODO 自动生成方法存根
60
61 }
62
63 public void ejbPassivate() throws EJBException, RemoteException {
64 // TODO 自动生成方法存根
65
66 }
67
68 /**
69 * An example business method
70 *
71 * @ejb.interface-method view-type = "remote"
72 *
73 * @throws EJBException Thrown if method fails due to system-level error.
74 */
75 public String hello() throws EJBException {
76 // rename and start putting your business logic here
77 return new String("HelloEJBWorld!");
78 }
79
80 }
81
其实就是修改了其中的一个方法:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
/**
2 * An example business method
3 *
4 * @ejb.interface-method view-type = "remote"
5 *
6 * @throws EJBException Thrown if method fails due to system-level error.
7 */
8 public String hello() throws EJBException {
9 // rename and start putting your business logic here
10 return new String( " HelloEJBWorld! " );
11 }
注意:代码中的解释文字不要删除,因为XDoclet需要。
2 * An example business method
3 *
4 * @ejb.interface-method view-type = "remote"
5 *
6 * @throws EJBException Thrown if method fails due to system-level error.
7 */
8 public String hello() throws EJBException {
9 // rename and start putting your business logic here
10 return new String( " HelloEJBWorld! " );
11 }
配置属性
![ejb6.jpg](https://i-blog.csdnimg.cn/blog_migrate/07d8a9e00a722ab6875482ad51313fb5.jpeg)
添加weblogic.jar。我的路径是:bea\weblogic90\server\lib\weblogic.jar
![ejb16.jpg](https://i-blog.csdnimg.cn/blog_migrate/dd786d9842b3f1f04c8d3100d9ccb98c.jpeg)
![ejb7.jpg](https://i-blog.csdnimg.cn/blog_migrate/50dfc48d8a56686d6506133683ace2e9.jpeg)
![ejb8.jpg](https://i-blog.csdnimg.cn/blog_migrate/8ddd9e2da6e02945b690282c05cdfe41.jpeg)
![ejb9.jpg](https://i-blog.csdnimg.cn/blog_migrate/b940cc3cfa0c74224a0eea429c8f42ad.jpeg)
![ejb10.jpg](https://i-blog.csdnimg.cn/blog_migrate/9106dbf34164b96680400d1cff88b911.jpeg)
![ejb11.jpg](https://i-blog.csdnimg.cn/blog_migrate/3453819cbc949263df7acf1d72bd86dc.jpeg)
![ejb12.jpg](https://i-blog.csdnimg.cn/blog_migrate/0731cff4dd2ea0373e68cfd51f71db21.jpeg)
![ejb13.jpg](https://i-blog.csdnimg.cn/blog_migrate/b3f7778da3634488c1101188c29160e4.jpeg)
![ejb14.jpg](https://i-blog.csdnimg.cn/blog_migrate/6faa0a14a8b5827c5d9aaefca7ef6ace.jpeg)
![ejb15.jpg](https://i-blog.csdnimg.cn/blog_migrate/b781cb3878f4de27a5f22cd52827d3eb.jpeg)
就下来写EJBTest类:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
package
com;
2
3 import java.rmi.RemoteException;
4 import java.util.Properties;
5
6 import javax.ejb.CreateException;
7 import javax.naming.Context;
8 import javax.naming.InitialContext;
9 import javax.naming.NamingException;
10
11 import com.interfaces.HelloWorld;
12 import com.interfaces.HelloWorldHome;
13
14 public class EJBTest {
15
16 /**
17 * @param args
18 */
19 public static void main(String[] args) {
20 // TODO 自动生成方法存根
21 Properties properties = new Properties();
22 properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, " weblogic.jndi.WLInitialContextFactory " );
23 properties.setProperty(Context.PROVIDER_URL, " t3://localhost:7001 " );
24
25 Context context;
26 try {
27 context = new InitialContext(properties);
28 HelloWorldHome hwh = (HelloWorldHome)context.lookup( " ejb/HelloWorld " );
29 HelloWorld hw = hwh.create();
30 String s = hw.hello();
31 System.out.println(s);
32 } catch (NamingException e) {
33 // TODO 自动生成 catch 块
34 e.printStackTrace();
35 } catch (RemoteException e) {
36 // TODO 自动生成 catch 块
37 e.printStackTrace();
38 } catch (CreateException e) {
39 // TODO 自动生成 catch 块
40 e.printStackTrace();
41 }
42
43 }
44
45
46 }
47
2
3 import java.rmi.RemoteException;
4 import java.util.Properties;
5
6 import javax.ejb.CreateException;
7 import javax.naming.Context;
8 import javax.naming.InitialContext;
9 import javax.naming.NamingException;
10
11 import com.interfaces.HelloWorld;
12 import com.interfaces.HelloWorldHome;
13
14 public class EJBTest {
15
16 /**
17 * @param args
18 */
19 public static void main(String[] args) {
20 // TODO 自动生成方法存根
21 Properties properties = new Properties();
22 properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, " weblogic.jndi.WLInitialContextFactory " );
23 properties.setProperty(Context.PROVIDER_URL, " t3://localhost:7001 " );
24
25 Context context;
26 try {
27 context = new InitialContext(properties);
28 HelloWorldHome hwh = (HelloWorldHome)context.lookup( " ejb/HelloWorld " );
29 HelloWorld hw = hwh.create();
30 String s = hw.hello();
31 System.out.println(s);
32 } catch (NamingException e) {
33 // TODO 自动生成 catch 块
34 e.printStackTrace();
35 } catch (RemoteException e) {
36 // TODO 自动生成 catch 块
37 e.printStackTrace();
38 } catch (CreateException e) {
39 // TODO 自动生成 catch 块
40 e.printStackTrace();
41 }
42
43 }
44
45
46 }
47
最后就是看结果了,先启动weblogic,然后运行EJBTest程序。
![ejb17.jpg](https://i-blog.csdnimg.cn/blog_migrate/9b3aa6c973af59630cd29c924a031746.jpeg)