Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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启动-从application.yml访问值_Java_Spring_Spring Boot - Fatal编程技术网

Java Spring启动-从application.yml访问值

Java Spring启动-从application.yml访问值,java,spring,spring-boot,Java,Spring,Spring Boot,我创建了一个带有indexName属性的可索引注释,该注释必须放在类的顶部,我想为我在application.yml文件中定义的indexName添加一个前缀 application: elasticsearch: prefix: dev_ @Indexable(indexName = "${application.elasticsearch.prefix}address") public class Address implements Serializable { } @R

我创建了一个带有indexName属性的可索引注释,该注释必须放在类的顶部,我想为我在application.yml文件中定义的indexName添加一个前缀

application:
  elasticsearch:
    prefix: dev_

@Indexable(indexName = "${application.elasticsearch.prefix}address")
public class Address implements Serializable {

}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Indexable {

  String indexName() default "";

}
在要设置值的字段上使用@Value(${yaml.path.to.Value}”)注释。例如,对于yaml.path.to.value:


yaml:
  path:
    to:
     value: someValue

使用yml时要小心空格。

您尝试过这个吗?@ConfigurationProperties获取类内应用程序文件中的值,但我想获取类上方的值。请查看。我相信这会有所帮助。解决方案可能有所不同,并且取决于jdk版本。在这个调整的帮助下,您将能够在运行时更改注释值。thnx@jahra^^,我发现jdk版本存在问题,但我在这里找到了解决方案