Eclipse Dependency Injection

A java class has a dependency on another class if it uses it as a variable. We want to avoid creating instances of other classes with the new operator because that hard codes the dependency on that class.

Instead we use the @Inject notation, this can inject variables in:

Example

Example from IBM DI Tutorial:

 
@Inject
public FrogMan(Vehicle vehicle) {
  this.vehicle = vehicle;
}

Where Vehicle is an interface.

 
@Inject
public FrogMobile(FuelSource fuelSource){
  this.fuelSource = fuelSource;
}

Modules

Example of module from IBM DI Tutorial:
module

These modules are used by Guice.createInjector to create the injector.


metadata block
see also:
Correspondence about this page

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2022 Martin John Baker - All rights reserved - privacy policy.