Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.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 使用@Value访问Spring Boot Actuator build.version属性_Java_Spring_Spring Boot_Spring Boot Actuator - Fatal编程技术网

Java 使用@Value访问Spring Boot Actuator build.version属性

Java 使用@Value访问Spring Boot Actuator build.version属性,java,spring,spring-boot,spring-boot-actuator,Java,Spring,Spring Boot,Spring Boot Actuator,这应该很容易,但不起作用。 我在我的弹簧靴(2.0.1-RELEASE)应用程序中激活了弹簧靴执行器 执行器端点/exactor/info按预期工作,并显示正确的版本信息。文件build info.properties已存在 尝试使用访问版本属性时(例如,在我的Spring控制器类中): 操作失败,错误为无法解析值“${build.version}”中的占位符“build.version” 有什么建议吗?我需要将文件build info.properties添加为@PropertSource @

这应该很容易,但不起作用。 我在我的弹簧靴(2.0.1-RELEASE)应用程序中激活了弹簧靴执行器

执行器端点
/exactor/info
按预期工作,并显示正确的版本信息。文件
build info.properties
已存在

尝试使用访问版本属性时(例如,在我的Spring控制器类中):

操作失败,错误为
无法解析值“${build.version}”
中的占位符“build.version”


有什么建议吗?

我需要将文件
build info.properties
添加为
@PropertSource

@SpringBootApplication()
@PropertySource("classpath:META-INF/build-info.properties")
public class MyApp implements WebMvcConfigurer { 
  [...]
}
然后可以在注释中使用构建信息

@SomeAnnotation(value = "${build.version}")
public class someClass { ... }

使用spring表达式语言,它非常简单和干净

@Value("#{buildProperties.get('version')}")           // not 'build.version'
private String myAppBuildVersion;
或者更好的方法是,
Autowire
buildProperties
bean直接连接到您的组件,这样您就可以随心所欲地使用它了

@Autowired
private BuildProperties buildProperties;
注意:
autoconfiguration
去掉了
build.
前缀。那么你的
SpEL
表达式应使用
version
作为键。不是
build.version


在@Service中,将@Value作为构造函数的参数放入构造函数中。不要使用独立值并尝试引用它

像这样:

@Service
public class myService {

  public myService(@Value("${my.param}") String myParam) {
   client.withParam(myParam).build();
  }

}
其中application.properties的值如下:

my.param=http://google.com
我尝试过其他的实现,但都不起作用。比如说,

 @Service
 public class myService {

      @Value("${my.param}")
      String myParam;

      public myService() {
       client.withParam(myParam).build();
      }

  }
不起作用


在这种情况下,服务将被初始化,但字符串将为null。我无法解释。我想这与param构造和bean初始化时间有关

谢谢!这是前进的方向,正如预期的那样。
 @Service
 public class myService {

      @Value("${my.param}")
      String myParam;

      public myService() {
       client.withParam(myParam).build();
      }

  }