Java Reference
In-Depth Information
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class SequenceGenerator {
@Autowired
@Qualifier("datePrefixGenerator")
private PrefixGenerator prefixGenerator;
...
}
Then Spring will attempt to find a bean with that name in the IoC container and wire it into the
property.
<bean id=" datePrefixGenerator"
class="com.apress.springenterpriserecipes.sequence.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd" />
</bean>
The @Qualifier annotation can also be applied to a method argument for auto-wiring.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class SequenceGenerator {
...
@Autowired
public void inject(
@Qualifier("datePrefixGenerator") PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}
}
You can create a custom qualifier annotation type for the auto-wiring purpose. This annotation
type must be annotated with @Qualifier itself. This is useful if you want a specific type of bean and
configuration injected wherever an annotation decorates a field or setter method.
package com.apress.springenterpriserecipes.sequence;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;import
org.springframework.beans.factory.annotation.Qualifier;
Search WWH ::




Custom Search