@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
{ ... }
新闻热点
疑难解答