Spring 从@value访问从后端java对象加载的属性

Spring 从@value访问从后端java对象加载的属性,spring,spring-boot,properties,spring-boot-starter,Spring,Spring Boot,Properties,Spring Boot Starter,我正在编写定制的spring boot starter项目,其中我们有自己的类VaultFactory,它根据application.yml文件中定义的用户属性连接到vault 在CustomStarter项目中,我可以读取所有机密和相应的值。现在,我想将所有这些键/值对公开为属性源,以便消费者可以通过@value直接访问 @Value{${some.vault.secret.name}} private String value; 我的初学者项目代码 @Configuration @Enab

我正在编写定制的spring boot starter项目,其中我们有自己的类VaultFactory,它根据application.yml文件中定义的用户属性连接到vault

在CustomStarter项目中,我可以读取所有机密和相应的值。现在,我想将所有这些键/值对公开为属性源,以便消费者可以通过@value直接访问

@Value{${some.vault.secret.name}}
private String value;
我的初学者项目代码

@Configuration
@EnableConfigurationProperties(VaultConfiguration.class)
@ConditionalOnClass(VaultFactory.class)
public class VaultEnableAutoConfiguration {

    /**
     * @param vaultConfiguration
     * @return
     * @throws VaultException 
     */
    @Bean
    @ConditionalOnMissingBean
    public Vault getVaultFactoryByProperties(VaultConfiguration vaultConfiguration) throws VaultException {
        VaultFactory vaultFactory = VaultFactory.newInstance(vaultConfiguration.getVault().get("file.path"), vaultConfiguration.getVaultProperties());
        return vaultFactory.getVault("vault-01");
    }
}

公共类VaultPropertySource扩展了PropertySource{
拱顶;
公共Vault属性源(字符串名称){
超级(姓名);
}
公共Vault属性源(字符串名称,Vault){
超级(姓名);
this.vault=vault;
}
@凌驾
公共对象getProperty(字符串名称){
试一试{
返回vault.getSecret(name.getValue();
}捕获(e){
e、 printStackTrace();
}
返回null;
}
}
@ConfigurationProperties(“平台”)
公共类保险库配置{
私有最终地图库=新HashMap();
公共地图getVault(){
返回拱顶;
}
公共属性getVaultProperties(){
属性p=新属性();
vault.entrySet().stream().forEach(e->p.put(e.getKey(),e.getValue());
返回p;
}
}
我怎么做?基本上,若我的初学者项目中有一个键/值映射,那个么我如何通过@value注释使这些可以启动应用程序呢

面临多重问题:

  • 始终首先调用Vault PropertiesConfiguration,并且@EnableConfigurationProperties和@autowired不起作用

  • 我硬编码以解决上述问题,但它试图从propertysource加载spring.messages.always-use-message-format属性,而我没有这个属性


  • 不能将
    @Value
    .yml
    文件一起使用,方法与
    .properties
    文件相同


    你能更详细地解释一下“我能读懂所有的秘密和相应的值”的含义吗。那么它在应用程序中是如何可用的呢?

    只需在环境中注册一个新的
    PropertySource
    ,或者为您的案例创建一个专用的
    属性源(就像Spirng Cloud Config那样),然后注册。@M.Deinum我尝试在我的定制初学者项目中添加属性源,但我面临一些问题。您能告诉我我做错了什么吗?很抱歉,在custom starter中,我们有自己的vault Factory类,它连接到vault并获取数据。我想将这些数据作为@value公开给引导应用程序(使用自定义启动程序的应用程序)
    
    @EnableConfigurationProperties(VaultConfiguration.class)
    public class VaultPropertiesConfiguration {
    
        @Autowired
        Vault vault;
    
        @Bean
        public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(VaultConfiguration vaultConfiguration) throws VaultException {
    
            PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
            ConfigurableEnvironment environment = new StandardEnvironment();
            MutablePropertySources ps = environment.getPropertySources();
    
            pspc.setIgnoreUnresolvablePlaceholders(Boolean.TRUE);
            pspc.setIgnoreUnresolvablePlaceholders(true);
            ps.addLast(new VaultPropertySource("vaultProperties", vault));
            pspc.setPropertySources(ps);
            return pspc;
        }
    
    }
    
    public class VaultPropertySource extends PropertySource<Vault>{
    
        Vault vault;
    
        public VaultPropertySource(String name) {
            super(name);
        }
    
        public VaultPropertySource(String name, Vault vault) {
            super(name);
            this.vault = vault;
        }
    
        @Override
        public Object getProperty(String name) {
            try {
                return vault.getSecret(name).getValue();
            } catch (VaultException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    
    @ConfigurationProperties("platform")
    public class VaultConfiguration {
    
        private final Map<String, String> vault = new HashMap<>();
    
        public Map<String, String> getVault() {
            return vault;
        }
    
        public Properties getVaultProperties() {
            Properties p = new Properties();
            vault.entrySet().stream().forEach( e -> p.put(e.getKey(), e.getValue()));
            return p;
        }
    
    }