无法使Spring的ImportAware工作

vltsax25  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(51)

我试图创建自己的@ EnableXXX风格的annotation(即@EnableCustomizedPropertySources)。为此,annotation导入CustomizedPropertySourcesConfiguration类,后者又实现了ImportAware,以便访问@EnableCustomizedPropertySources annotation的属性。
注解:

@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 annotation注解一些@Configuration class时,Spring不会调用setImportMetadata方法,因此我无法访问annotations属性。

xxslljrj

xxslljrj1#

ImportAware类(CustomizedPropertySourcesConfiguration)需要以下两个:

@Configuration
@Component

字符串

相关问题