我是 Guice 的新手,我正在将它用于具有大量遗留代码的应用程序。它有几个类,如下所示:
public final class DataAccessClass {
private Transaction txn;
@Inject //This was just added
public DataAccessClass(/* injectable parameters */, Transaction txn) {
this.txn = txn;
}
//Maybe add @Inject here to set a new transaction when it changes?
public void setTransaction(Transaction txn) {
this.txn = txn;
}
public void writeData(/* parameters for data to be written */) {
//Write data using the current instance of 'txn'
}
}
很清楚如何使用 Guice 来绑定永不改变的实例,但是改变的实例(即事务)呢?有没有办法让我在更改时使用 Guice 注入不同的 Transaction 实例? 请注意,事务实例不是受良好支持的 JPA/Hibernate/Spring 事务之一
我能想到的侵入性最小的方法(避免一次迁移每个使用事务的类)将仅在实例化对象时使用 Guice 注入事务,并且我会保留在必要时更新事务的现有应用程序代码. 例如,此 Provider 可用于使用当前 Transaction 实例注入新对象:
public final class TransactionProvider implements Provider<Transaction> {
/** Nullable transaction that should be used for all operations at the moment */
private Transaction txn;
public TransactionProvider(Transaction txn) {
this.txn = txn;
}
/**
* @param txn Nullable transaction to use for all operations at the moment
*/
public void setTransaction(Transaction txn) {
this.txn = txn;
}
/* Provider methods */
public Transaction get() {
return this.txn;
}
}
应用程序逻辑看起来像这样:
public final class Application {
private final Provider<Transaction> transactionProvider;
private final DataAccessClass dao; //Instance provided by Guice
public void scopedOperation() {
//Use a fresh transaction for this operation
final Transaction txn = ...;
//Make transaction available to Guice (for new objects) and to legacy (already-instantiated) DAO classes
this.transactionProvider.setTransaction(txn);
this.dao.setTransaction(txn); //Legacy approach - can this be updated?
//Do some kind of data operation under the scope of the current transaction
try {
this.dao.writeData(...);
} catch (Exception e) {
txn.abort();
throw new RuntimeException("Operation failed", e);
}
//The operation is over now
this.txn.commit();
}
是否有其他方法可以更新现有类使用的 Transaction 实例,而无需一次迁移每个类?