我是Guice的新手,我正在使用大量遗留代码处理应用程序。它有几个类,如下所示:迁移应用程序以使用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在事务更改时注入另一个事务实例? 请注意,交易实例不是很好的支持JPA /休眠/ Spring的事务中的一个
最小侵入性的方法我能想到的(避免一次使用事务需要迁移的每级)只有在实例化对象时才会使用Guice来注入事务,并且我会保留现有的应用程序代码,以便在必要时更新事务。例如,这提供可用于有交易的当前实例注入新的对象:
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();
}
还有没有其他的方法来更新实例现有类使用的事务而不必一次迁移每个类?
谢谢 - 这看起来像它会起作用。重构遗留类以请求Guice的事务的好处 - 它看起来只是对每个遗留类的小改动,而且它可能比使用2个系统来管理事务更容易。 – 2009-06-08 15:17:46