自定义弹簧(引导)注释:带有默认键名的@ConditionalOnProperty

时间:2018-01-22 10:23:39

标签: java spring spring-boot

如何创建自定义的Spring(引导)注释@ConditionalOnProperty"继承" interface WizardService { void doMagic() } ,但预先定义了属性键?

给定一些共享通用接口的服务

@ConditionalOnProperty(name = "country", havingValue = "[iso-code]")

以及通过@ConditionalOnProperty(name = "country", havingValue = "de") @Service class WizardServiceGermany { @Override void doMagic() { System.out.println("Simsalabim!"); } } @ConditionalOnProperty(name = "country", havingValue = "en") @Service class WizardServiceGreatBritain { @Override void doMagic() { System.out.println("Wingardium Leviosa!"); } } 选择的一组国家/地区特定实施,即根据国家/地区属性的值

选择实施
name

是否可以定义自定义弹簧注释,该注释始终将@Country属性设置为" country"默认情况下,我可以有一个@Country("en") @Service class WizardServiceGreatBritain { @Override void doMagic() { System.out.println("Wingardium Leviosa!"); } } 注释?例如:

@ConditionalOnProperty

我尝试创建一个元注释,但它会被忽略(当用它的@ConditionalOnProperty(name = "country") @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface Country { @AliasFor(annotation = ConditionalOnProperty.class, attribute = "havingValue") String havingValue() default ""; } 等效替换它时):

col1    col2    col3    group
1        2      100  alpha
1        2      100  alpha
1        2      100  alpha
3        4      200  beta
3        4      200  beta
3        4      200  beta
3        4      300  beta
5        6      700  charlie
7        8      400  tango
7        8      300  tango
7        8      700  tango

0 个答案:

没有答案