0
votes

I'm working on setting up a Spring "meta-annotation" similar to the SpringBootApplication Spring Boot provides. I've been able to setup ComponentScan and EnableAutoConfiguration annotations using this convention successfully, however I'm not able to pass the value attribute successfully on to ImportResource.

Here's a quick example of what I'd like to do:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ComponentScan(basePackages = {"com.my.package.example"})
@EnableAutoConfiguration
@ImportResource
public @interface MyMetaAnnotationExample {
    String[] value() default {};
}

However, the compiler complains that no attribute is passed to ImportResource. When I try passing that attribute, I don't have the values I want passed through the MyMetaAnnotationExample so I'm not able to set these.

The ComponentScan works of course because that is something that is desired to hard-coded, but the ImportResource is something that is desired to pass the value in.

1

1 Answers

2
votes

Spring Framework 4.2 (used by Spring Boot 1.3.x) has relaxed the restriction on @ImportResource so you should be able to do what you want.

You can use @AliasFor on your meta-annotation to specify that its value attribute should configure the value attribute on ImportResource. For example:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ComponentScan(basePackages = {"com.my.package.example"})
@EnableAutoConfiguration
@ImportResource
public @interface MyMetaAnnotationExample {

    @AliasFor(annotation=ImportResource.class, attribute="value")
    String[] value() default {};
}