迁移应用程序以使用 Guice - 如何将事务注入现有对象?

发布于 2024-07-22 19:36:47 字数 2453 浏览 4 评论 0原文

我是 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 绑定永不改变的实例,但是那些发生改变的实例(即事务)呢? 有没有办法让我在 Transaction 发生变化时使用 Guice 注入不同的 Transaction 实例? 请注意,Transaction 实例不是受良好支持的 JPA/Hibernate/Spring 事务之一

我能想到的侵入性最小的方法(避免需要一次迁移每个使用事务的类) )只会在实例化对象时使用 Guice 注入 Transaction,并且我会保留在必要时更新事务的现有应用程序代码。 例如,此 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 实例,而无需立即迁移每个类?

I'm new to Guice, and I'm working it in to an application with a large amount of legacy code. It has several classes that look like this:

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'
    }
}

It's pretty clear how to use Guice to bind instances that never change, but what about instances that do change (i.e. transactions)? Is there a way for me to use Guice to inject a different instance of Transaction when it changes? Note that the Transaction instance is not one of the well-supported JPA/Hibernate/Spring transactions

The least invasive approach I can think of (avoiding the need to migrate each and every class that uses a transaction at once) would use Guice to inject Transaction only when instantiating objects, and I'd keep the existing application code that updates transactions when necessary. For example, this Provider could be used to inject new objects with the current instance of 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;
    }
}

Application logic would look something like this:

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();
    }

Are there other ways to update the instance of Transaction that existing classes use without having to migrate each class at once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月夜 2024-07-29 19:36:47

如果我理解正确的话,你的问题有两个独立的部分:

  1. 在 Guice-fied 类上使用正确的 Transaction 实例,
  2. 在遗留类上使用正确的 Transaction 实例。

对于“1”,您的自定义提供程序可以工作,但我会为事务创建自定义范围并在该范围绑定 Transaction 类。 请参阅 http://code.google.com/p/google-guice/wiki /CustomScopes 有关自定义范围的说明。

关于“2”,一旦您有了自定义范围,这就是使用 Guice 向遗留类提供实例的常见问题。 您没有提到当前向遗留类提供 Transaction 实例的代码,但您可以更改该特定代码段以从 Guice 请求实例。 由于您位于“事务范围”内,Guice 会负责提供正确的实例。

If I understand this correctly, your problem has two independent parts:

  1. using the right Transaction instance on Guice-fied classes
  2. using the right Transaction instance on legacy classes.

For '1', your custom provider works, but I would create a custom scope for a transaction and bind the Transaction class at that scope. See http://code.google.com/p/google-guice/wiki/CustomScopes for instructions about custom scopes.

Regarding '2', once you have a custom scope, this is the usual problem of using Guice to provide instances to legacy classes. You didn't mention the code that currently provides Transaction instances to legacy classes, but you could just change that particular piece of code to request an instance from Guice. Since you are inside your "transaction scope", Guice takes care of providing the right instance.

痴情 2024-07-29 19:36:47

请查看辅助注入。 要使用它,定义一个三行接口:

public interface DataAccessClassFactory {
  DataAccessClass create(Transaction transaction);
}

...然后使用 FactoryProvider 绑定来绑定工厂:

bind(DataAccessClassFactory.class).toProvider(
    FactoryProvider.newFactory(DataAccessClassFactory.class, DataAccessClass.class));

然后您可以在需要构造的任何地方注入 DataAccessClassFactory一个DataAccessClass。 AssistedInject 包含在 Guice 2 中,尽管它需要单独的 .jar 文件。

Take a look at Assisted Inject. To use it, define a three-line interface:

public interface DataAccessClassFactory {
  DataAccessClass create(Transaction transaction);
}

...and then use a FactoryProvider binding to bind the factory:

bind(DataAccessClassFactory.class).toProvider(
    FactoryProvider.newFactory(DataAccessClassFactory.class, DataAccessClass.class));

Then you can inject a DataAccessClassFactory wherever you need to construct a DataAccessClass. AssistedInject is included in Guice 2, although it requires a separate .jar file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文