@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);
}
}
這個簡單方面所做的全部是,從一個屬性文件(這個邏輯被封裝在dependencymanager對象中)查詢實現類并且在存取字段之前把它注入到用@resource注解所注解的字段中。顯然,這種實現不是完整的,但是它確實說明了你可以怎樣以一種jsr 250兼容方式且不需采用ejb來提供資源注入。
四、 安全性
除了資源注入外,jsr 250和ejb 3.0還提供經由注解的元數據安全表示。javax.annotation.security包定義了五個注解-runas,rolesallowed,permitall,denyall和rolesreferenced-所有這些都能應用到方法上來定義安全要求。例如,如果你想要聲明上面列出的bookflight方法僅能為具有"user"角色的調用者所執行,那么你可以用如下的安全約束來注解這個方法:
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)
{ ... }
}
五、 事務
事務成為企業開發的一個重要部分-因為它們有助于在一個并發的環境中的數據集成。從一個高層次上看,事務可以通過多種或者是完整的或者是都不完整的操作來保證這一點。
不象針對資源注入和安全的注解,針對事務的注解是特定于ejb 3.0的并且沒有在jsr 250普通注解中定義。ejb 3.0定義了兩個與事務相聯系的注解:transactionmanagement和transactionattribute。該transactionmanager注解指定事務是由容器所管理還是為bean所管理的。在ejb 3中,如果這個注解沒被指定,那么將使用容器所管理的事務。transactionattribute注解用于指定方法的事務傳播級別。有效值-包括強制的、要求的、要求新的、支持的、不支持的和從不支持的-用來定義是否要求一個已有事務或啟動一個新的事務,等等。
因為bookflight操作包含兩步-訂購一個外出航班和一個返回航班,所以,通過把它包裝成一個事務,你能保證這項操作的一致性。通過使用ejb 3.0事務注解,這將看上去如下所示:
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
{ ... }
新聞熱點
疑難解答