我正在使用 Spring-Boot 2.4.0 和org.springframework.cloud:spring-cloud-starter-vault-config:3.0.0
启动成功后发现如下
@Bean
public Object see(@Value("${credentials}") String secretFromVault, VaultTemplate tpl) throws URISyntaxException {
VaultResponse resp = tpl.read("secret/data/myapp");
System.out.println("kv entries = " + resp.getData()); // This successfully show the secret
System.out.println("secret = " + secretFromVault); // Sadly, this returns null
return "hello";
}
所以,我尝试了另一种更简单的方法来通过定义这个来读取值:
@Configuration
@VaultPropertySource("secret/data/myapp")
public class VaultConfig {
@Autowired
Environment env;
@Bean
public Object testBean() {
System.out.println(env.getProperty("credentials") + "===============================");
return new ArrayList<>();
}
}
结果更糟,应用程序甚至无法启动。
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.vault.config.VaultAutoConfiguration]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.cloud.vault.config.VaultAutoConfiguration.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1310) ~[spring-beans-5.3.1.jar:5.3.1]
... 46 common frames omitted
Caused by: java.lang.NoSuchMethodException: org.springframework.cloud.vault.config.VaultAutoConfiguration.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3349) ~[na:na]
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.3.1.jar:5.3.1]
... 47 common frames omitted