class RL extends DefaultRecordListener {
@Override
public void insertEnd(RecordContext ctx) {
// Put some property into the data map
ctx.configuration().data("x", "y");
}
}
class TL extends DefaultTransactionListener {
String x;
@Override
public void commitEnd(TransactionContext ctx) {
// Retrieve the property again
x = (String) ctx.configuration().data("x");
}
}
然后可以如下使用:
RL rl = new RL();
TL tl = new TL();
ctx.configuration()
.derive(rl)
.derive(tl)
.dsl()
.transaction(c -> {
assertNull(c.data("x"));
TRecord t = c.dsl().newRecord(T);
t.setA("a");
t.setB("b");
// insertEnd() triggered here
assertEquals(1, t.insert());
assertEquals("y", c.data("x"));
// commitEnd() triggered here
});
// Since the transaction creates a nested, derived scope, you don't see these things
// from the outside of the transaction in your possibly global Configuration
assertNull(ctx.data("x"));
assertNull(ctx.configuration().data("x"));
assertEquals("y", tl.x);
1条答案
按热度按时间bnl4lu3b1#
您可以通过访问
Configuration.data()
为此目的而创建的属性。考虑以下两个听众:然后可以如下使用: