使用ECLIPSE 创建local EJB应用

JAVA EE 7.0允许local EJB以war形式打包。

java ee tutorial这本书是以netbean 工具编写的。

我用Eclipse环境下实践了一下,在websphere application server Liberty服务器环境下测试。

ConverterBean


 

Creating a Dynamic Web Project


 

To create a project, complete the following steps:

_1. Begin by creating a new project to hold the EJB application. Click File → New → Dynamic

Web Project. On the first page of the New Project wizard, enter ConverterBean in the

Project name field.


 


 

扫描二维码关注公众号,回复: 4511426 查看本文章

_2. Make sure that WebSphere Application Server V8.5 Liberty Profile is selected for the

Target run time. Also, ensure that the Add project to an EAR check box is cleared.


 

_3. Click Finish to complete the wizard. A new project named ConverterBean is created after

you exit the wizard.


 

Creating the EJB

After creating the project, complete the following steps to create the EJB:

_1. From the menu, select File → New → Session Bean (EJB 3.x), which starts the Create

EJB 3.x Session Bean wizard.

_2. In the first window of the wizard, ensure that ConverterBean is selected in the Project field,

enter javaeetutorial.converter.ejb in the Java package field, and ConverterBean in the Class name field.

Ensure that Stateless is specified for the State type field, and default No-interface View

is selected. When you select the No-interface View, all public, non-static methods of the

bean are available to clients. If you do not want that availability, select Local and define

the interface with only required methods.


 

_3. Click Next to advance to the next window, and then click Finish. The wizard now creates a

class named ConverterBean. Notice that the class is annotated with @Stateless and @LocalBean.

_4. Now, complete the EJB implementation by adding a method that is named dollarToYen and yenToEuro



 

package javaeetutorial.converter.ejb;

import java.math.BigDecimal;

import javax.ejb.Stateless;

/**

* This is the bean class for the ConverterBean enterprise bean.

* @author ian

*/

@Stateless

public class ConverterBean {

private final BigDecimal yenRate = new BigDecimal("104.34");

private final BigDecimal euroRate = new BigDecimal("0.007");

public BigDecimal dollarToYen(BigDecimal dollars) {

BigDecimal result = dollars.multiply(yenRate);

return result.setScale(2, BigDecimal.ROUND_UP);

}

public BigDecimal yenToEuro(BigDecimal yen) {

BigDecimal result = yen.multiply(euroRate);

return result.setScale(2, BigDecimal.ROUND_UP);

}

}


 


 

Create the web Client


 


 

A servlet is used to test the created bean. To create a servlet, complete the following steps:

_1. Using the Create New Servlet wizard, create a servlet in the ConverterBean project, in the

package javaeetutorial.converter.web named ConverterServlet. In the generated class, add a field

named ConverterBean of type converter. Annotate the field with @EJB.

_2. In the servlet’s doGet method, print the results of calling the hello method on helloBean.


 


 

Running the applications using Eclipse


 

You can test ConverterServlet directly from the workspace:

Select the ConverterServlet, right-click, and select Run as → Run on Server

Browser in Eclipse is automatically opened pointing to the following website:

http://localhost:9080/convertBean/


 


 

Result:

You should see the output, Servlet ConverterServlet at /convertBean, in your web browser.

This shows that the EJB was automatically injected into the helloBean field and that the

servlet was able to successfully start the method hello.


 


 



 

正在 Java HotSpot(TM) 64-Bit Server VM V1.8.0_65-b17 (zh_CN) 上启动 server1 (WebSphere Application Server 17.0.0.1/wlp-1.0.16.cl170120170227-0220)

[AUDIT ] CWWKE0001I: 已启动服务器 server1。

[AUDIT ] CWWKE0100I: 此产品允许用于开发,但限制生产使用。可在此处查看完整的许可条款:https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/license/base_ilan/ilan/17.0.0.1/lafiles/zh.html

[AUDIT ] CWWKZ0058I: 正在监视应用程序的 dropins。

[AUDIT ] CWWKI0001I: 现在 CORBA 名称服务器在 corbaloc:iiop:localhost:2809/NameService 处可用。

[AUDIT ] CWWKT0016I: Web 应用程序可用 (default_host):http://localhost:9080/ibm/adminCenter/javaBatch-1.0/

[AUDIT ] CWWKT0016I: Web 应用程序可用 (default_host):http://localhost:9080/ibm/adminCenter/explore-1.0/

[AUDIT ] CWWKT0016I: Web 应用程序可用 (default_host):http://localhost:9080/IBMJMXConnectorREST/

[AUDIT ] CWWKT0016I: Web 应用程序可用 (default_host):http://localhost:9080/ibm/adminCenter/serverConfig-1.0/

[AUDIT ] CWWKT0016I: Web 应用程序可用 (default_host):http://localhost:9080/ibm/api/

[AUDIT ] CWWKT0016I: Web 应用程序可用 (default_host):http://localhost:9080/adminCenter/

[AUDIT ] CWWKT0016I: Web 应用程序可用 (default_host):http://localhost:9080/convertBean/

[AUDIT ] CWWKZ0001I: 应用程序 convertBean 已在 5.244 秒内启动。

[AUDIT ] CWWKF0012I: 服务器已安装下列功能部件:[servlet-3.1, beanValidation-1.1, ssl-1.0, jndi-1.0, jca-1.7, batchManagement-1.0, ejbPersistentTimer-3.2, appSecurity-2.0, j2eeManagement-1.1, jdbc-4.1, wasJmsServer-1.0, jaxrs-2.0, javaMail-1.5, adminCenter-1.0, cdi-1.2, webProfile-7.0, jcaInboundSecurity-1.0, jpa-2.1, jsp-2.3, ejbLite-3.2, managedBeans-1.0, jsf-2.2, ejbHome-3.2, jaxws-2.2, localConnector-1.0, jsonp-1.0, restConnector-1.0, el-3.0, jaxrsClient-2.0, concurrent-1.0, appClientSupport-1.0, ejbRemote-3.2, javaee-7.0, jaxb-2.2, mdb-3.2, jacc-1.5, batch-1.0, ejb-3.2, json-1.0, jaspic-1.1, distributedMap-1.0, websocket-1.1, wasJmsSecurity-1.0, wasJmsClient-2.0]。

[AUDIT ] CWWKF0011I: 服务器 server1 已准备就绪,可开始运行智慧地球。


 

猜你喜欢

转载自blog.csdn.net/openeis/article/details/83022421
EJB
今日推荐