private Dependency dependency;
public void performLookup(Container container) {
this.dependency = (Dependency) container.getDependency("myDependency");
}
}
Note that in Listing 4-4, Dependency is an empty class.
Constructor Dependency Injection
Constructor Dependency Injection is Dependency Injection where a component's dependencies are
provided to it in its constructor(s). The component declares a constructor or a set of constructors taking
as arguments its dependencies, and the IoC container passes the dependencies to the component when
it instantiates it, as shown in Listing 4-5.
Listing 4-5. Constructor Dependency Injection
package com.apress.prospring3.ch4;
public class ConstructorInjection {
private Dependency dependency;
public ConstructorInjection(Dependency dependency) {
this.dependency = dependency;
}
}
Setter Dependency Injection
In Setter Dependency Injection, the IoC container injects a component's dependencies into the
component via JavaBean-style setter methods. A component's setters expose the set of dependencies the
IoC container can manage. Listing 4-6 shows a typical Setter Dependency Injection­based component.
Listing 4-6. Setter Dependency Injection
package com.apress.prospring3.ch4;
public class SetterInjection {
private Dependency dependency;
public void setDependency(Dependency dependency) {
this.dependency = dependency;
}
}
Within the container, the dependency requirement exposed by the setDependency() method is
referred to by the JavaBeans-style name, dependency. In practice, Setter Injection is the most widely used
injection mechanism, and it is one of the simplest IoC mechanisms to implement.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home