[英]How can we utilize the custom @Qualifier methods?
I have multiple questions related to how annotation methods are implemented/utilized.我有多个关于如何实现/使用注释方法的问题。
How can we utilize methods of a qualifier?我们如何利用限定符的方法? Let's say I have a custom @Qualifier
ie.假设我有一个自定义@Qualifier
即。 @Store
which represents an AppStore, and has 2 methods. @Store
代表一个 AppStore,有 2 个方法。 How can I use the values passed to DeviceType deviceType()
?如何使用传递给DeviceType deviceType()
的值?
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Store {
String value();
DeviceType deviceType() default DeviceType.Phone;
public enum DeviceType{
Phone, Tablet;
}
} }
How does @Qualifier
internally uses the String value()
for matching the name? @Qualifier
如何在内部使用String value()
来匹配名称? Is the same being overridden by the String value()
when creating a custom @Qualifier
?创建自定义@Qualifier
时是否会被String value()
覆盖?
--- UPDATED INFO BELOW --- ---更新信息如下---
I have a service that contains a field of type AppStore, which has an @Autowired and @Store annotation.我有一个包含 AppStore 类型字段的服务,该字段具有 @Autowired 和 @Store 注释。 How can i access the deviceType passes in the annotation after getting a bean of iPadAppStoreService?获取 iPadAppStoreService 的 bean 后,如何访问注解中的 deviceType 传递?
@Service
public class iPadAppStoreService {
....
@Autowired(required=false)
@Store(value = "appleAppStore",
deviceType = DeviceType.Tablet)
private AppStore applicationStore;
....
}
How can we utilize methods of a qualifier?我们如何利用限定符的方法?
You call them.你打电话给他们。
Let's say I have a custom
@Qualifier
ie.假设我有一个自定义@Qualifier
即。@Store
which represents an AppStore, and has 2 methods.@Store
代表一个 AppStore,有 2 个方法。 How can I use the values passed toDeviceType deviceType()
?如何使用传递给DeviceType deviceType()
的值?
Store storeAnnotation = clazz.getAnnotation(Store.class);
DeviceType deviceType = storeAnnotation.deviceType();
where clazz
is a class that has been annotated with @Store
.其中clazz
是已用 @Store 注释的@Store
。
How does
@Qualifier
internally uses theString value()
for matching the name?@Qualifier
如何在内部使用String value()
来匹配名称?
If an @Autowired
field/method/parameter finds multiple candidates by type and there is also a @Qualifier
annotation (or a derived annotation like @Store
), it will look for a candidate type that has been annotated with @Qualifier
with a matching value
element.如果@Autowired
字段/方法/参数按类型找到多个候选者,并且还有一个@Qualifier
注释(或类似@Store
的派生注释),它将查找已使用@Qualifier
注释并具有匹配value
的候选类型元素。
Is the same being overridden by the
String value()
when creating a custom@Qualifier
?创建自定义@Qualifier
时是否会被String value()
覆盖?
Yes, the @Store
annotation will be treated the same way as the @Qualifier
annotation, ie its element values will be used the same way.是的, @Store
注释的处理方式与@Qualifier
注释相同,即其元素值的使用方式相同。 Any extra element values are ignored by Spring, and will only be used by code you add. Spring 会忽略任何额外的元素值,并且只会被您添加的代码使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.