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

时间:2015-11-02 14:12:20

标签: java spring spring-annotations

我们正在使用自定义限定符注释来创建和注入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?

1 个答案:

答案 0 :(得分:0)

其中一种方法可能是创建Map Platform个bean并将其注入调用getDevice的bean中。地图的关键可以是设备类型。

沿着相同的方向的另一种方法可能是使你的bean实现InitializingBeanApplicationContextAware并在'afterPropertiesSet in conjuction with findAnnotationOnBean`中使用'getBeansWithAnnotation'来填充Map或查找bean动态。