Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:如何将值注入到由自定义注释注释的字段中_Java_Spring - Fatal编程技术网

Java Spring:如何将值注入到由自定义注释注释的字段中

Java Spring:如何将值注入到由自定义注释注释的字段中,java,spring,Java,Spring,我的自定义批注: @Target({ElementType.FIELD, ElementType.PARAMETER}) @Documented @Retention(RetentionPolicy.RUNTIME) public @interface KmsProperty { String key() default ""; } 豆子: 我想将值注入到由@KmsProperty注释的所有字段中,这些值来自不同的来源(属性文件或第三方密钥管理服务),我如何在应用程序启动时做到这一点?

我的自定义批注:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface KmsProperty {
    String key() default "";
}
豆子:


我想将值注入到由
@KmsProperty
注释的所有字段中,这些值来自不同的来源(属性文件或第三方密钥管理服务),我如何在应用程序启动时做到这一点?

为什么不简单地使用
@Value
?为什么需要专门的自定义注释?@M.Deinum,因为它的值来自不同的来源,具体取决于当前的环境。例如,在local env中,它的值来自application-local.yaml,但在prd env中,我需要调用第三方api来获取和设置它的值。使用
@value
仍然无法实现任何功能。注册一个定制的
PropertySource
(不是实际实现的注释),它可以实现这一点。这与SpringCloudConfig的工作方式相同(通过注册特定的
PropertySource
实例,从git、hasicorp vault等获取信息)。我建议使用它,而不是试图栓住自己的其他解决方案。为什么不简单地使用
@Value
?为什么需要专门的自定义注释?@M.Deinum,因为它的值来自不同的来源,具体取决于当前的环境。例如,在local env中,它的值来自application-local.yaml,但在prd env中,我需要调用第三方api来获取和设置它的值。使用
@value
仍然无法实现任何功能。注册一个定制的
PropertySource
(不是实际实现的注释),它可以实现这一点。这与SpringCloudConfig的工作方式相同(通过注册特定的
PropertySource
实例,从git、hasicorp vault等获取信息)。我建议你使用这个方法,而不是试图套牢你自己的其他解决方案。
public class Prop {
    @KmsProperty(key = ApiKeyConstant.SERVICE_SECRET)
    private String serviceSecret;
}