Monday, 19 March 2012

Runtime Bean Wiring

Sproing provides an easy method of wiring an object into Spring at runtime when that object hasn't been created by Spring but needs access to Spring beans.

The SpringWirer class provides a static method that, given an object, will wire it into the existing application context and set the spring beans needed

public class Jedi {
  private Force force;
  public Jedi() {
    // wire this object up
    SpringWirer.wireBean(this);
    force.push();
  }
  // set the Force spring bean
  public void setForce(Force force) {
    this.force = force;
  }

In this example, the Force object can be accessed because it is a bean defined in Spring. At runtime, the SpringWirer.wireBean(this) call will wire the object into Spring, setting the Force object using the setter method

Sproing version 0.3.2 is available now from Sith Open Source

No comments:

Post a Comment