迁移应用程序以使用Guice

编程入门 行业动态 更新时间:2024-10-20 04:03:42
迁移应用程序以使用Guice - 如何将事务注入到现有对象中?(Migrating application to use Guice - how to inject transactions into existing objects?)

我是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来注入事务,并且会保留现有的应用程序代码,以便在必要时更新事务。 例如,此提供程序可用于使用当前的事务实例注入新对象:

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

是否有其他方法可以更新现有类使用的事务实例,而无需一次迁移每个类?

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?

最满意答案

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

在Guice-fied类上使用正确的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:

using the right Transaction instance on Guice-fied classes 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.

更多推荐

本文发布于:2023-08-05 00:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Guice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!