@stateful
public class cartbean implements shoppingcart
{
private float total;
private vector productcodes;
public int someshoppingmethod(){...};
...
}
public class travelagencyserviceimpl implements itravelagencyservice
{
public iflightdao flightdao;
public travelagencyserviceimpl()
{ flightdao = flightdaofactory.getinstance().getflightdao(); }
public void booktrip(long outboundflightid, long returnflightid, int seats)
throws insufficientseatsexception
{
reserveseats(outboundflightid, seats);
reserveseats(returnflightid, seats);
}
}
public class travelagencyserviceimpl implements itravelagencyservice
{
@resource(name = "flightdao")
public iflightdao flightdao;
public void booktrip(long outboundflightid, long returnflightid, int seats)
throws insufficientseatsexception
{
reserveseats(outboundflightid, seats);
reserveseats(returnflightid, seats);
}
}
@aspect
public class injectionaspect
{
private dependencymanager manager = new dependencymanager();
@before("get(@resource * *.*)")
public void beforefieldaccesses(joinpoint thisjoinpoint)
throws illegalargumentexception, illegalaccessexception
{
fieldsignature signature = (fieldsignature) thisjoinpoint.getsignature();
resource injectannotation = signature.getfield().getannotation(resource.class);
object dependency = manager.resolvedependency(signature.getfieldtype(),injectannotation.name());
signature.getfield().set(thisjoinpoint.getthis(), dependency);
}
}
這個(gè)簡(jiǎn)單方面所做的全部是,從一個(gè)屬性文件(這個(gè)邏輯被封裝在dependencymanager對(duì)象中)查詢實(shí)現(xiàn)類并且在存取字段之前把它注入到用@resource注解所注解的字段中。顯然,這種實(shí)現(xiàn)不是完整的,但是它確實(shí)說明了你可以怎樣以一種jsr 250兼容方式且不需采用ejb來提供資源注入。
四、 安全性
除了資源注入外,jsr 250和ejb 3.0還提供經(jīng)由注解的元數(shù)據(jù)安全表示。javax.annotation.security包定義了五個(gè)注解-runas,rolesallowed,permitall,denyall和rolesreferenced-所有這些都能應(yīng)用到方法上來定義安全要求。例如,如果你想要聲明上面列出的bookflight方法僅能為具有"user"角色的調(diào)用者所執(zhí)行,那么你可以用如下的安全約束來注解這個(gè)方法:
public class travelagencyserviceimpl implements itravelagencyservice
{
@resource(name = "flightdao")
public iflightdao flightdao;
@rolesallowed("user")
public void booktrip(long outboundflightid, long returnflightid, int seats)
throws insufficientseatsexception
{
reserveseats(outboundflightid, seats);
reserveseats(returnflightid, seats);
}
}
@aspect
public class securityaspect
{
@around("execution(@javax.annotation.security.rolesallowed * *.*(..))")
public object aroundsecuredmethods(proceedingjoinpoint thisjoinpoint)
throws throwable
{
boolean callerauthorized = false;
rolesallowed rolesallowed = rolesallowedforjoinpoint(thisjoinpoint);
for (string role : rolesallowed.value())
{
if (callerinrole(role))
{ callerauthorized = true; }
}
if (callerauthorized)
{ return thisjoinpoint.proceed(); }
else
{
throw new runtimeexception("caller not authorized to perform specified function");
}
}
private rolesallowed rolesallowedforjoinpoint(proceedingjoinpoint thisjoinpoint)
{
methodsignature methodsignature = (methodsignature) thisjoinpoint.getsignature();
method targetmethod = methodsignature.getmethod();
return targetmethod.getannotation(rolesallowed.class);
}
private boolean callerinrole(string role)
{ ... }
}
五、 事務(wù)
事務(wù)成為企業(yè)開發(fā)的一個(gè)重要部分-因?yàn)樗鼈冇兄谠谝粋€(gè)并發(fā)的環(huán)境中的數(shù)據(jù)集成。從一個(gè)高層次上看,事務(wù)可以通過多種或者是完整的或者是都不完整的操作來保證這一點(diǎn)。
不象針對(duì)資源注入和安全的注解,針對(duì)事務(wù)的注解是特定于ejb 3.0的并且沒有在jsr 250普通注解中定義。ejb 3.0定義了兩個(gè)與事務(wù)相聯(lián)系的注解:transactionmanagement和transactionattribute。該transactionmanager注解指定事務(wù)是由容器所管理還是為bean所管理的。在ejb 3中,如果這個(gè)注解沒被指定,那么將使用容器所管理的事務(wù)。transactionattribute注解用于指定方法的事務(wù)傳播級(jí)別。有效值-包括強(qiáng)制的、要求的、要求新的、支持的、不支持的和從不支持的-用來定義是否要求一個(gè)已有事務(wù)或啟動(dòng)一個(gè)新的事務(wù),等等。
因?yàn)閎ookflight操作包含兩步-訂購一個(gè)外出航班和一個(gè)返回航班,所以,通過把它包裝成一個(gè)事務(wù),你能保證這項(xiàng)操作的一致性。通過使用ejb 3.0事務(wù)注解,這將看上去如下所示:
public class travelagencyserviceimpl implements itravelagencyservice
{
@resource(name = "flightdao")
public iflightdao flightdao;
@rolesallowed("user")
@transactionattribute(transactionattributetype.required)
public void booktrip(long outboundflightid, long returnflightid, int seats)
throws insufficientseatsexception
{
reserveseats(outboundflightid, seats);
reserveseats(returnflightid, seats);
}
}
@aspect
public class transactionaspect
{
@pointcut("execution(@javax.ejb.transactionattribute * *.*(..))")
public void transactionalmethods(){}
@before("transactionalmethods()")
public void beforetransactionalmethods()
{ hibernateutil.begintransaction(); }
@afterreturning("transactionalmethods()")
public void afterreturningtransactionalmethods()
{ hibernateutil.committransaction(); }
@afterthrowing("transactionalmethods()")
public void afterthrowingtransactionalmethods()
{ hibernateutil.rollbacktransaction(); }
}
@stateful
public class travelagencyserviceimpl implements itravelagencyservice
{ ... }
|
新聞熱點(diǎn)
疑難解答
圖片精選