Java 如何使用自定义限定符注释动态获取bean
我们使用自定义限定符注释来创建和注入bean。我们如何在运行时通过指定自定义限定符来动态选择bean 自定义限定符:Java 如何使用自定义限定符注释动态获取bean,java,spring,spring-annotations,Java,Spring,Spring Annotations,我们使用自定义限定符注释来创建和注入bean。我们如何在运行时通过指定自定义限定符来动态选择bean 自定义限定符: @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @Qualifier public @interface PlatformQualifiers {
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE,
ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface PlatformQualifiers {
public static enum OperatingSystems {
IOS, ANDROID
}
OperatingSystems operatingSystem() default OperatingSystems.IOS;
public enum DeviceTypes {
Mobile, Tablet, ANY, Other
}
DeviceTypes[] deviceType() default { DeviceTypes.ANY };
}
Bean接口:
@FunctionalInterface
public interface Platform {
String getDeviceDetails();
}
Bean配置:
@Configuration
public class PlatformConfig {
@Bean
@PlatformQualifiers(operatingSystem = OperatingSystems.IOS, deviceType = DeviceTypes.Mobile)
public Platform getIphone6() {
return () -> "iphone6";
}
@Bean
@PlatformQualifiers(operatingSystem = OperatingSystems.IOS, deviceType = DeviceTypes.Tablet)
public Platform getIpad() {
return () -> "ipad3";
}
@Bean
@PlatformQualifiers(operatingSystem = OperatingSystems.ANDROID, deviceType = DeviceTypes.Mobile)
public Platform getAndroidPhone() {
return () -> "AndroidPhoneSamsung";
}
}
当前应用程序代码:
@Configuration
@ComponentScan
public class MainApplication {
@Autowired
@PlatformQualifiers(operatingSystem = OperatingSystems.IOS, deviceType = DeviceTypes.Mobile)
Platform iphone;
@Autowired
@PlatformQualifiers(operatingSystem = OperatingSystems.ANDROID, deviceType = DeviceTypes.Mobile)
Platform androidPhone;
public void getDevice(String osType, String deviceType ) {
if(osType == "ios" && deviceType == "mobile") {
System.out.println(iphone.getDeviceDetails());
}
if(osType == "android" && deviceType == "mobile") {
System.out.println(androidPhone.getDeviceDetails());
}
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainApplication.class);
MainApplication mainApplication = context.getBean(MainApplication.class);
mainApplication.getDevice("ios" ,"mobile");
mainApplication.getDevice("android" , "mobile");
}
}
我正在寻找一种解决方案,比如在运行时我可以在何处使用限定符访问bean,如下所示:
@Configuration
@ComponentScan
public class MainApplication2 {
@Autowired
ApplicationContext context;
public void getDevice(DeviceTypes deviceType, OperatingSystems osType ) {
>>>>>>>>>>> Looking of something of type following :
Platform p = context.getBean(some input consisting to identify bean by deviceType and osType)
System.out.println(p.getDeviceDetails());
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainApplication2.class);
MainApplication2 application = context.getBean(MainApplication2.class);
application.getDevice(DeviceTypes.Mobile, OperatingSystems.ANDROID);
}
}
在这种情况下,如何基于设备类型和操作系统在运行时从applicationContext获取bean 其中一种方法是创建
平台
bean的映射
,并将其注入调用getDevice
的bean中。映射的键可以是设备类型
同样的另一种方法是让你的bean实现
initializebean
和ApplicationContextAware
,并在afterPropertiesSet中使用'getBeansWithAnnotation'与
FindNotationOnBean'结合使用,以动态地填充映射或查找bean。四年后仍然没有接近CDI优雅的解决方案。。。难以置信的