概要
JBoss 應用程序服務器(AS)
在
這篇文章中
讓我們從目前EJB
(開源和J
(一)EJB
開始的時候
為了證明以上觀點
圖
從示例源碼包ejb
//localhost
為何要使用EJB?
對一個如此簡單的應用為什麼使用EJB?EJB容器提供了一些有用的服務
在該分支下嘛
該應用有兩個EJB構件
Calculator bean使用了History實體bean訪問數據庫
圖
圖
如你所看到的
JBoss AS
(二)簡單化的創新
從使用EJB危險中
在過去的幾年
兩個JBoss贊助的開源項目在它的POJO中間件框架中扮演了重要的角色
為了支持企業級的應用
企業POJO的強大之處
POJO抵押金計算器應用也包含在源碼包裡
//localhost
就如我講論述的
重用注解
注解是從J
以下列出了替代了Calculator會話Bean的Calculator POJO類的基本骨架
@SecurityDomain (
public class Calculator {
@Unchecked
public Calculator () {
}
@Permissions ({
@Tx (TxType
public double getPayment (int principal
int term) throws Exception {
// Calculate and save to database
// Code omitted for clarity
}
@Permissions ({
@Tx (TxType
public List getHistory (double payment) throws Exception {
// Search the database
// Code omitted for clarity
}
}
@SecurityDomain 注解聲明了該POJO的安全域是other
但是
/**
* @@org
*/
public class Calculator {
/**
* @@org
*/
public Calculator () {
}
/**
* @@org
* @@org
*/
public double getPayment (int principal
int term) throws Exception {
// Calculate and save to database
// Code omitted for clarity
}
/**
* @@org
* @@org
*/
public List getHistory (double payment) throws Exception {
// Search the database
// Code omitted for clarity
}
}
要使用JBoss AOP注解編譯器
<target name=
<taskdef name=
classname=
classpat />
</target>
<target name=
<annotationc compilerclasspat
classpath=
bytecode=
<src path=
</annotationc>
</target>
有了Java注解
輕量級的POJO持久化
容器管理持久化實體bean的主要功能是模擬應用數據和保持其對後端數據庫的持續透明
History類是一個在計算事務中模擬數據的簡單POJO
public class History {
private int id;
private int principal;
private double rate;
private int term;
private double payment;
public History () {
}
public History (int principal
int term
this
this
this
this
}
public int getId () {
return id;
}
public void setId (int id) {
this
}
public int getPrincipal () {
return principal;
}
public void setPrincipal (int principal) {
this
}
public double getRate () {
return rate;
}
public void setRate (double rate) {
this
}
public int getTerm () {
return term;
}
public void setTerm (int term) {
this
}
public double getPayment () {
return payment;
}
public void setPayment (double payment) {
this
}
}
現在我們需要一個名為
<hibernate
<class name=
table=
<id name=
<generator class=
</id>
<property name=
type=
<property name=
type=
<property name=
type=
<property name=
type=
</class>
</hibernate
但是我們還沒指定用哪個後端數據庫和如何與JBoss Server容器集成
<server>
<mbean code=
name=
<attribute name=
java:/DefaultDS
</attribute>
<attribute name=
net
</attribute>
<attribute name=
java:/hibernate/SessionFactory
</attribute>
<attribute name=
net
</attribute>
<attribute name=
create
</attribute>
</mbean>
</server>
要在JBoss AS中部署Hibernate模塊
<target name=
<jar jarfile=
<metainf dir=
<fileset dir=
<include name=
<include name=
</fileset>
</jar>
</target>
JBoss
Hibernate模塊模擬了業務數據
在一個正規的Hibernate應用裡
然而
對於抵押金計算器POJO應用
以下列出的展示了在Calculator POJO類裡的Hibernate相關代碼
public class Calculator {
/**
* @@org
*/
public Calculator () {
}
/**
* @@org
* @@org
*/
public double getPayment (int principal
int term) throws Exception {
rate = rate /
rate = rate /
double tmp = Math
tmp = (principal * tmp * rate) / (tmp
// Save this calculation into the database
// Notice that it is automatically associated
// with the AOP transaction
// to close the session!
try {
History history = new History (principal
rate *
Session hsess =
HibernateContext
hsess
} catch (Exception e) {
e
// The new exception triggers the tx rollback
throw new Exception (
}
return tmp;
}
/**
* @@org
* @@org
*/
public List getHistory (double payment) throws Exception {
List result = null;
try {
Session hsess =
HibernateContext
result = hsess
new Double(payment)
} catch (Exception e) {
e
// The new exception triggers the tx rollback
throw new Exception (
}
return result;
}
}
總結
圖
(三)前瞻EJB
基於POJO中間件的理念植根於開源項目
EJB
如果今日你想與明日的EJB
核對你的JBoss版本
請確保你有安裝正確JBoss版本支持EJB
為了展示EJB
//localhost
如下代碼所示
@Local
public interface Calculator {
public double getPayment (int principal
// Get previous queries that has payment lower than
// the current one
public List getHistory (double payment);
}
CalculatorBean類是會話Bean的實現
@Stateless
@SecurityDomain(
public class CalculatorBean implements Calculator {
@Inject
private EntityManager manager;
public CalculatorBean () { }
@MethodPermissions({
@Tx(TxType
public double getPayment (int principal
rate = rate /
rate = rate /
double tmp = Math
tmp = (principal * tmp * rate) / (tmp
HistoryBean history =
new HistoryBean (principal
manager
return tmp;
}
@MethodPermissions({
@Tx(TxType
public List getHistory (double payment) {
return manager
}
}
HistoryBean類是對數據庫進行持久化的數據模型
@Entity
@Table(name =
public class HistoryBean {
private int id;
private int principal;
private double rate;
private int term;
private double payment;
public HistoryBean () {
}
public HistoryBean (int principal
int term
this
this
this
this
}
@Id(generate = GeneratorType
public int getId () {
return id;
}
public void setId (int id) {
this
}
public int getPrincipal () {
return principal;
}
public void setPrincipal (int principal) {
this
}
public double getRate () {
return rate;
}
public void setRate (double rate) {
this
}
public int getTerm () {
return term;
}
public void setTerm (int term) {
this
}
public double getPayment () {
return payment;
}
public void setPayment (double payment) {
this
}
}
總的來說
圖
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27980.html