2 回答
TA贡献1801条经验 获得超8个赞
如果所有不同的是您在 中所做的TransactionCallback,只需将TransactionCallback作为参数传递给执行“样板事务”的方法,然后执行TransactionUtils:
private static Foo doMyDatabaseStuff(ApplicationContext context, WebSite webSite, TransactionCallback cb) {
//A. Do a bunch of boilerplate Transaction stuff
TransactionUtils transactionUtils = getTransactionUtils(context);
return (Foo) transactionUtils.execute(cb);
}
并从您现有的方法调用此方法:
public static Foo doMyDatabaseStuff(ApplicationContext context, WebSite webSite) {
return doMyDatabaseStuff(context, webSite, new TransactionCallback() {
public Object doInTransaction(TransactionStatus ts) {
//B. Do a bunch of use case specific DB stuff
FooDao fooDao = (FooDao) context.getBean("FooDao");
Foo foo = fooDao.create(webSite);
{
foo.setName("Administratoren");
foo.setSite(webSite);
//call a bunch of other setters
fooDao.update(foo);
return foo;
}
}
});
}
添加回答
举报