Java Reference
In-Depth Information
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
public class SequenceGenerator {
@Autowired
private PrefixGenerator prefixGenerator;
...
}
You may even apply the @Autowired annotation to a method with an arbitrary name and an arbitrary
number of arguments. Then Spring will attempt to wire a bean with the compatible type for each of the
method arguments.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
public class SequenceGenerator {
...
@Autowired
public void inject (PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}
}
Auto-Wiring All Beans of Compatible Type
The @Autowired annotation can also be applied to a property of array type to have Spring auto-wire all
the matching beans. For example, you can annotate a PrefixGenerator[] property with @Autowired .
Then Spring will auto-wire all the beans whose type is compatible with PrefixGenerator at one time.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
public class SequenceGenerator {
@Autowired
private PrefixGenerator[] prefixGenerators;
...
}
If you have multiple beans whose type is compatible with the PrefixGenerator defined in the IoC
container, they will be added to the prefixGenerators array automatically.
Search WWH ::




Custom Search