2015-12-17 55 views
0

我正在设置类似于SpringBootApplication Spring Boot提供的Spring“元注释”。我已经能够使用这个约定成功地设置ComponentScan和EnableAutoConfiguration注解,但是我无法成功地将值属性传递给ImportResource。有没有人知道如何在“元注释”中使用Spring的ImportResource注释?

这里是想我做一个简单的例子:

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

然而,编译器会抱怨,没有属性被传递到ImportResource。当我尝试传递该属性时,我没有希望通过MyMetaAnnotationExample传递的值,因此我无法设置这些值。

课程的ComponentScan作品,因为这是需要硬编码的东西,但ImportResource是希望通过在一些有价值的东西。

回答

0

Spring框架4.2(在Spring 1.3.x的引导)放宽了对@ImportResource的限制,所以你应该可以做你想做的。

您可以使用@AliasFor你的元注释指定其value属性应该ImportResource配置value属性。例如:

@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 {}; 
}