我正在尝试创建自己的@EnableXxx样式注释(即@EnableCustomizedPropertySources)。为此,注释导入类CustomizedPropertySourcesConfiguration,后者又实现ImportAware,以便访问@EnableCustomizedPropertySources注释的属性。
注释:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(CustomizedPropertySourcesConfiguration.class)
public @interface EnableCustomizedPropertySources {
String externalFolderName();
String propertiesFileName();
(...)
}
导入的配置类:
@Configuration
public class CustomizedPropertySourcesConfiguration implements ImportAware {
protected AnnotationAttributes enableCustomizedPropertySourcesAttributes;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> attributes = importMetadata.getAnnotationAttributes(EnableCustomizedPropertySources.class.getName(), false);
this.enableCustomizedPropertySourcesAttributes = AnnotationAttributes.fromMap(attributes);
}
@Bean
public PropertySourcesPlaceholderConfigurer propertySource() {
return (...);
}
}
问题是,当我使用@EnableCustomizedPropertySources注释注释某些@Configuration类时,Spring不会调用方法setImportMetadata,因此我无法访问注释属性。
答案 0 :(得分:0)
ImportAware类(CustomizedPropertySourcesConfiguration)需要:
@Configuration
@Component