程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Jboss EJB 3.0--Stateless Beans

Jboss EJB 3.0--Stateless Beans

編輯:關於JAVA

Jboss EJB 3.0

Stateless Beans

Calculator.Java // 計算器的接口:加、減

package org.jboss.tutorial.stateless.bean;

public interface Calculator

{

int add(int x, int y);

int suBTract(int x, int y);

}

CalculatorBean.Java

package org.jboss.tutorial.stateless.bean;

import Javax.ejb.Stateless;

@Stateless //標記CalculatorBean 為 stateless bean,為本地和遠程接口的實現

public class CalculatorBean implements CalculatorRemote, CalculatorLocal

{

public int add(int x, int y)

{

return x + y;

}

public int suBTract(int x, int y)

{

return x - y;

}

}

CalculatorLocal.Java

package org.jboss.tutorial.stateless.bean;

import Javax.ejb.Local;

@Local //標記為本地接口,繼承了計算器接口

public interface CalculatorLocal extends Calculator

{

}

CalculatorRemote.Java

package org.jboss.tutorial.stateless.bean;

import Javax.ejb.Remote;

@Remote //標記為遠程接口,繼承了計算器接口

public interface CalculatorRemote extends Calculator

{

}

客戶端

ClIEnt.Java

package org.jboss.tutorial.stateless.clIEnt;

import org.jboss.tutorial.stateless.bean.Calculator;

import org.jboss.tutorial.stateless.bean.CalculatorRemote;

import Javax.naming.InitialContext;

public class ClIEnt

{

public static void main(String[] args) throws Exception

{

InitialContext ctx = new InitialContext();

//查找遠程接口

Calculator calculator = (Calculator) ctx.lookup(CalculatorRemote.class.getName());

System.out.println("1 + 1 = " + calculator.add(1, 1));

System.out.println("1 - 1 = " + calculator.suBTract(1, 1));

}

}

*****************************簡化版********************************

只需三個程序即可實現StatelessBean功能。

Calculator.Java

package org.jboss.tutorial.stateless.bean;

import Javax.ejb.Remote;

@Remote //直接將計算器標記為遠程接口

public interface Calculator

{

int add(int x, int y);

int suBTract(int x, int y);

}

CalculatorBean.Java

package org.jboss.tutorial.stateless.bean;

import Javax.ejb.Stateless;

@Stateless

public class CalculatorBean implements Calculator //這裡實現了計算器接口

{

public int add(int x, int y)

{

return x + y;

}

public int suBTract(int x, int y)

{

return x - y;

}

}

客戶端:

ClIEnt.Java

package org.jboss.tutorial.stateless.clIEnt;

import org.jboss.tutorial.stateless.bean.Calculator;

import Javax.naming.InitialContext;

public class ClIEnt

{

public static void main(String[] args) throws Exception

{

InitialContext ctx = new InitialContext();

Calculator calculator = (Calculator) ctx.lookup(Calculator.class.getName());

//只要查找標記為remote的接口就行了

System.out.println("1 + 1 = " + calculator.add(1, 1));

System.out.println("1 - 1 = " + calculator.suBTract(1, 1));

}

}

這裡附上log4j.properties 在jboss-EJB-3.0_PrevIEw_5.zip 裡面沒有這個老是顯示缺少appender

log4j.propertIEs

log4j.appender.R=org.apache.log4j.RollingFileAppender

log4j.appender.R.File=record.log

log4j.appender.R.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=%p %d{hh:mm:ss} %t %c{1} -%m%n

log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.MaxFileSize=100KB

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.rootLogger=stdout,R

運行:參考installing.Html

Windows下

打開命令提示符cmd,到 jboss_home/bin

Run.bat –c all

用ant

先build後run 就行了。

討論:

簡化版是看到jboss-EJB-3.0_PrevIEw_5.zip裡面的statefull源碼而改寫的,在EJB spe 3.0說好像是刪除了home這個接口,我覺得簡化很多接口更方面,有效的開發J2EE application

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved