См. этот ответ , почему это работает ...
@SpringBootApplication
public class So56189689Application {
public static void main(String[] args) {
SpringApplication.run(So56189689Application.class, args);
}
@Value("#{containsObject('foo') ? getObject('foo').foo : null}")
String foo;
@Bean
public ApplicationRunner runner() {
return args -> System.out.println(foo);
}
// @Bean
// public Foo foo() {
// return new Foo();
// }
public static class Foo {
private String foo = "bar";
public String getFoo() {
return this.foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
}
EDIT
Объект #root
выражения SpEL - это BeanExpressionContext
, в этом контексте вы можете вызывать методы containsObject()
и getObject()
.
Вот код из BeanExpressionContext
:
public boolean containsObject(String key) {
return (this.beanFactory.containsBean(key) ||
(this.scope != null && this.scope.resolveContextualObject(key) != null));
}
public Object getObject(String key) {
if (this.beanFactory.containsBean(key)) {
return this.beanFactory.getBean(key);
}
else if (this.scope != null){
return this.scope.resolveContextualObject(key);
}
else {
return null;
}
}