[weld-dev] Automatic non contextual injection using bytecode instrumentation

Stuart Douglas stuart.w.douglas at gmail.com
Sun Nov 14 17:35:55 EST 2010


I just had an idea for something that I think would be rather cool,
and allow objects created using new to be injected automatically.

Conceptually it would look something like this:

@AutoInject
public class AccountService
{
  private final Account account;

  @Inject
  private PaymentProcessor paymentProcessor;

  AccountService(Account account)
  {
    this.account = account;
  }

  public void doPayment(double amount)
  {
    paymentProcessor.processPayment(account, amount);
  }
}

We could then create the object using new and the fields will still be injected:

AccountService a = new AccountService(account);
a.doPayment(10);

To implement this will require a javaagent, which will modify the
bytecode of @AutoInject annotated classes as they are loaded, so that
the constructor bytecode looks up the values to inject and sets the
injected field values appropriately. Would this be something that is
worth putting into weld extensions?

Stuart


More information about the weld-dev mailing list