使用 Spring @Value 元注释的注释不注入值
Annotation with Spring @Value Meta-annotation not injecting the value
拥有属性文件和必要的 context:properties-placeholder
配置...
someproperty=somevalue
在一个 Spring bean 中我们可以有:
@Value("${someproperty}")
private String someProperty;
其中 someProperties 值将是 "somevalue"
。
我想用 @Value
创建一个新注释 @Refreshable
作为元注释,以便它的行为与直接使用 @Value
相同。由于 @Value
需要一个值,我硬编码 "default"
希望 @Refreshable's value()
会覆盖它:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("default")
public @interface Refreshable {
String value();
}
然后当我在 bean 中使用新注解时,我期望它能正常工作,注入声明的值 "somevalue"
:
@Refreshable("${someproperty}")
private String someProperty;
但我没有得到 ${someproperty}
的值,而是 "default"。想法?
已在4.3.0-BUILD-SNAPSHOT中解决。要覆盖@Value 的值,必须使用 @AliasFor
:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("")
public @interface Refreshable {
@AliasFor(annotation=Value.class, attribute="value")
String value();
}
拥有属性文件和必要的 context:properties-placeholder
配置...
someproperty=somevalue
在一个 Spring bean 中我们可以有:
@Value("${someproperty}")
private String someProperty;
其中 someProperties 值将是 "somevalue"
。
我想用 @Value
创建一个新注释 @Refreshable
作为元注释,以便它的行为与直接使用 @Value
相同。由于 @Value
需要一个值,我硬编码 "default"
希望 @Refreshable's value()
会覆盖它:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("default")
public @interface Refreshable {
String value();
}
然后当我在 bean 中使用新注解时,我期望它能正常工作,注入声明的值 "somevalue"
:
@Refreshable("${someproperty}")
private String someProperty;
但我没有得到 ${someproperty}
的值,而是 "default"。想法?
已在4.3.0-BUILD-SNAPSHOT中解决。要覆盖@Value 的值,必须使用 @AliasFor
:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("")
public @interface Refreshable {
@AliasFor(annotation=Value.class, attribute="value")
String value();
}