如何使用自定义限定符注释动态获取bean



我们使用自定义限定符注释来创建和注入bean。我们如何在运行时通过指定自定义限定符来动态选择bean。

自定义限定符:

@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);
    }
}

在这种情况下,如何在基于DeviceTypes和OperatingSystems的运行时从applicationContext中获取bean?

其中一种方法可以是创建一个Platform bean的Map,并将其注入调用getDevice的bean中。映射的密钥可以是设备类型。

同样的另一种方法是让您的bean实现InitializingBeanApplicationContextAware,并在"afterPropertiesSet in conjuction with findAnnotationOnBean"中使用"getBeansWithAnnotation"来填充Map或动态查找bean。

最新更新