当前页面: 开发资料首页 → J2EE 专题 → 走向简单化之路--JBoss 4.0 简化了中间件的开发
摘要: 这篇文章中,我将用三个示例程序来展示JBoss AS 4.0中POJO中间件框架的简单性,以及他们是如何与当前和明日的J2EE规范关联起来的。如果你是一个JBoss的用户或者一个普通的J2EE开发人员,这篇文章将教你一些不仅在目前的JBoss AS 4.0和将来的JBoss 5.0 或者 J2EE 1.5 服务器上可以应用的轻便技巧。
@SecurityDomain ("other")
public class Calculator {
@Unchecked
public Calculator () {
}
@Permissions ({"AuthorizedUser"})
@Tx (TxType.REQUIRED)
public double getPayment (int principal, double rate,
int term) throws Exception {
// Calculate and save to database.
// Code omitted for clarity.
}
@Permissions ({"AuthorizedUser"})
@Tx (TxType.REQUIRED)
public List getHistory (double payment) throws Exception {
// Search the database.
// Code omitted for clarity.
}
}/**
* @@org.jboss.aspects.security.SecurityDomain ("other")
*/
public class Calculator {
/**
* @@org.jboss.aspects.security.Unchecked
*/
public Calculator () {
}
/**
* @@org.jboss.aspects.security.Permissions ({"AuthorizedUser"})
* @@org.jboss.aspects.tx.Tx (org.jboss.aspects.tx.TxType.REQUIRED)
*/
public double getPayment (int principal, double rate,
int term) throws Exception {
// Calculate and save to database.
// Code omitted for clarity.
}
/**
* @@org.jboss.aspects.security.Permissions ({"AuthorizedUser"})
* @@org.jboss.aspects.tx.Tx (org.jboss.aspects.tx.TxType.REQUIRED)
*/
public List getHistory (double payment) throws Exception {
// Search the database.
// Code omitted for clarity.
}
}
要使用JBoss AOP注解编译器,你仅需要在Ant的建造脚本里增加一个任务。以下列出了pojo-jdk14/build.xml 脚本中相关的部分。
... ...
classname="org.jboss.aop.ant.AnnotationC"
classpathref="build.classpath" />
... ...
classpath="${build.dir}/classes"
bytecode="true">
public class History {
private int id;
private int principal;
private double rate;
private int term;
private double payment;
public History () {
}
public History (int principal, double rate,
int term, double payment) {
this.principal = principal;
this.rate = rate;
this.term = term;
this.payment = payment;
}
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public int getPrincipal () {
return principal;
}
public void setPrincipal (int principal) {
this.principal = principal;
}
public double getRate () {
return rate;
}
public void setRate (double rate) {
this.rate = rate;
}
public int getTerm () {
return term;
}
public void setTerm (int term) {
this.term = term;
}
public double getPayment () {
return payment;
}
public void setPayment (double payment) {
this.payment = payment;
}
}
table="history">
type="int" column="principal"/>
type="double" column="rate"/>
type="int" column="term"/>
type="double" column="payment"/>
name="jboss.har:service=Hibernate">
java:/DefaultDS
net.sf.hibernate.dialect.HSQLDialect
java:/hibernate/SessionFactory
net.sf.hibernate.cache.HashtableCacheProvider
create-drop
public class Calculator {
/**
* @@org.jboss.aspects.security.Unchecked
*/
public Calculator () {
}
/**
* @@org.jboss.aspects.security.Permissions ({"AuthorizedUser"})
* @@org.jboss.aspects.tx.Tx (org.jboss.aspects.tx.TxType.REQUIRED)
*/
public double getPayment (int principal, double rate,
int term) throws Exception {
rate = rate / 100.;
rate = rate / 12.;
double tmp = Math.pow(1.+rate, term);
tmp = (principal * tmp * rate) / (tmp - 1.);
// Save this calculation into the database.
// Notice that it is automatically associated
// with the AOP transaction. We do not even need
// to close the session!
try {
History history = new History (principal,
rate * 1200., term, tmp);
Session hsess =
HibernateContext.getSession(
"java:/hibernate/SessionFactory");
hsess.save (history);
} catch (Exception e) {
e.printStackTrace ();
// The new exception triggers the tx rollback.
throw new Exception ("Saving failed!");
}
return tmp;
}
/**
* @@org.jboss.aspects.security.Permissions ({"AuthorizedUser"})
* @@org.jboss.aspects.tx.Tx (org.jboss.aspects.tx.TxType.REQUIRED)
*/
public List getHistory (double payment) throws Exception {
List result = null;
try {
Session hsess =
HibernateContext.getSession(
"java:/hibernate/SessionFactory");
result = hsess.find (
"from History as h where h.payment < ?",
new Double(payment), Hibernate.DOUBLE);
} catch (Exception e) {
e.printStackTrace ();
// The new exception triggers the tx rollback.
throw new Exception ("Finding failed!");
}
return result;
}
}@Local
public interface Calculator {
public double getPayment (int principal, double rate, int term);
// Get previous queries that has payment lower than
// the current one
public List getHistory (double payment);
}
@Stateless
@SecurityDomain("other")
public class CalculatorBean implements Calculator {
@Inject
private EntityManager manager;
public CalculatorBean () { }
@MethodPermissions({"AuthorizedUser"})
@Tx(TxType.REQUIRESNEW)
public double getPayment (int principal, double rate, int term) {
rate = rate / 100.;
rate = rate / 12.;
double tmp = Math.pow(1.+rate, term);
tmp = (principal * tmp * rate) / (tmp - 1.);
HistoryBean history =
new HistoryBean (principal, rate * 1200., term, tmp);
manager.create (history);
return tmp;
}
@MethodPermissions({"AuthorizedUser"})
@Tx(TxType.REQUIRESNEW)
public List getHistory (double payment) {
return manager.createQuery(
"from HistoryBean h where h.payment < :payment")
.setParameter("payment", new Double(payment))
.listResults();
}
}
@Entity
@Table(name = "history")
public class HistoryBean {
private int id;
private int principal;
private double rate;
private int term;
private double payment;
public HistoryBean () {
}
public HistoryBean (int principal, double rate,
int term, double payment) {
this.principal = principal;
this.rate = rate;
this.term = term;
this.payment = payment;
}
@Id(generate = GeneratorType.AUTO)
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
public int getPrincipal () {
return principal;
}
public void setPrincipal (int principal) {
this.principal = principal;
}
public double getRate () {
return rate;
}
public void setRate (double rate) {
this.rate = rate;
}
public int getTerm () {
return term;
}
public void setTerm (int term) {
this.term = term;
}
public double getPayment () {
return payment;
}
public void setPayment (double payment) {
this.payment = payment;
}
}