为@ConfigurationProperties

时间:2016-12-27 14:26:43

标签: java spring-boot

我尝试根据@ConfigurationProperties注释创建自定义元注释,但Spring似乎没有选择并解析@AliasFor注释。

这是我的注释:

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Bean
@ConfigurationProperties
public @interface ConfigurationBean {

    @AliasFor(annotation = ConfigurationProperties.class, attribute = "prefix")
    String value() default "";

    @AliasFor(annotation = Bean.class, attribute = "name")
    String[] beanName() default {};
}

我的申请类:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @ConfigurationBean("my.api")
    public RestApiClientProperties externalApiProperties() {
        return new RestApiClientProperties();
    }

    @Bean
    public Object foo(RestApiClientProperties properties) {
        System.out.println(properties.getBaseUrl()); // always null                        
        return "bar";
    }
}

我的属性类:

public class RestApiClientProperties {

    private String baseUrl;
    private Map<String,String> headers;

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }
}

我的application.properties:

my.api.base-url=https://my.api.path/foo/bar
my.api.headers.Content-Type=application/json

我是做错了还是Spring Boot不支持@AliasFor的{​​{1}}注释?

0 个答案:

没有答案