public class Component {
private String value;
public Component(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
import ch.mimo.swingglue.Adhesive;
public class ComponentAdhesive implements Adhesive {
// attribute ---------------------------------------------------------------
/**
* the attribute that will be populated by IoC
*/
private Component component;
// adhesive methods --------------------------------------------------------
/* (non-Javadoc)
* @see ch.mimo.swingglue.Adhesive#setup()
*/
public void setup() throws Exception {
System.out.println(this.getClass().getName() + ".setup()");
}
/* (non-Javadoc)
* @see ch.mimo.swingglue.Adhesive#adhere()
*/
public void adhere() throws Exception {
System.out.println(this.getClass().getName() + ".adhere() component value: " + component.getValue());
}
// getters and setters -----------------------------------------------------
/**
* @return Returns the component.
*/
public Component getComponent() {
return component;
}
/**
* @param component The component to set.
*/
public void setComponent(Component component) {
System.out.println(this.getClass().getName() + ".setComponent() invoked with value: " + component.getValue());
this.component = component;
}
}
String value = "The value that is stored in Component";
Component component = new Component(value);
GlueFactory factory = GlueFactory.getFactory();
Glue glue = factory.getGlue();
glue.addElement("component",component);
ComponentAdhesive adhesive = (ComponentAdhesive)glue.applyAdhesive(ComponentAdhesive.class);
String result = adhesive.getComponent().getValue();
// result == value
The Component Instance has to be registered as an element in the Glue instance. Inside the Glue sit's a weak hash map that holds that instance reference in relation to it's name.
glue.addElement("component",component);
The Adhesive class that implements the interface Adhesive is a bean style class. It can be consideret to be a POJO. If the Adhesive class, like in our example needs to have a dependency to the Component that we have registered in the Glue it has to do two things:
ComponentAdhesive adhesive = (ComponentAdhesive)glue.applyAdhesive(ComponentAdhesive.class);
That's all Folks...