Java Reference
In-Depth Information
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER })
@Qualifier
public @interface Generator {
String value();
}
Then you can apply this annotation to an @Autowired bean property. It will ask Spring to auto-wire
the bean with this qualifier annotation and the specified value.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
public class SequenceGenerator {
@Autowired
@Generator("prefix")
private PrefixGenerator prefixGenerator;
...
}
You have to provide this qualifier to the target bean that you want to be auto-wired into the
preceding property. The qualifier is added by the <qualifier> element with the type attribute. The
qualifier value is specified in the value attribute. The value attribute is mapped to the String value()
attribute of the annotation.
<bean id="datePrefixGenerator"
class="com.apress.springenterpriserecipes.sequence.DatePrefixGenerator">
<qualifier type="Generator" value="prefix" />
<property name="pattern" value="yyyyMMdd" />
</bean>
Auto-Wiring by Name
If you want to auto-wire bean properties by name, you can annotate a setter method, a constructor, or a
field with the JSR-250 @Resource annotation. By default, Spring will attempt to find a bean with the same
name as this property. But you can specify the bean name explicitly in its name attribute.
Note To use the JSR-250 annotations, you have to include common-annotations.jar (located in the
lib/j2ee directory of the Spring installation) in your classpath. However, if your application is running on
Java SE 6 or Java EE 5, you needn't include this JAR file.
Search WWH ::




Custom Search