Java 带有Spring@Value元注释的注释未注入值
有一个属性文件和必要的Java 带有Spring@Value元注释的注释未注入值,java,spring,annotations,Java,Spring,Annotations,有一个属性文件和必要的上下文:属性占位符配置 someproperty=somevalue 在SpringBean中,我们可以: @Value("${someproperty}") private String someProperty; 其中someProperties值将是“somevalue” 我想创建一个新的注释@Refreshable,将@Value作为元注释,这样它的行为与直接使用@Value相同。由于@Value需要一个值,我硬编码默认值希望@Refreshable的Value
上下文:属性占位符
配置
someproperty=somevalue
在SpringBean中,我们可以:
@Value("${someproperty}")
private String someProperty;
其中someProperties值将是“somevalue”
我想创建一个新的注释@Refreshable
,将@Value
作为元注释,这样它的行为与直接使用@Value
相同。由于@Value
需要一个值,我硬编码默认值希望@Refreshable的Value()
会覆盖它:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("default")
public @interface Refreshable {
String value();
}
然后,当我在bean中使用新注释时,我希望它能正常工作,注入声明的值“somevalue”
:
但是我没有得到${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();
}
您可以使用此值设置注释org.springframework.beans.factory.annotation.Value的值。相反,对于default value->public String value()default“default”,应该是这样的;关键是我不介意初始化的“默认”值。它的存在只是为了public@interface Refreshable
编译时不会出错。我真正想要的是,它的值来自新注释的实际使用@Refreshable(${someproperty}”)
。但是,使用此方法,${someproperty}占位符无法解析。您能否将显示如何配置/打印someproperty值的完整代码放在初始化问题上。这目前不受支持,好消息是已计划在4.3.0中使用:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("")
public @interface Refreshable {
@AliasFor(annotation=Value.class, attribute="value")
String value();
}