将动态值注入 bean 的 constructor-arg 标记

Injecting a dynamic value into a bean's constructor-arg tag

我有一个 3d 派对库,它将密码作为定义为 Spring bean 的 class 的构造函数参数。

<bean class="com.thirdparty.CoolClass" id="coolClassId">
   <constructor-arg index="1" value="clearTextPassword" />
</bean>

但是我有一个问题...安全策略禁止我使用明文密码。所以我可以设置另一个接受加密密码并对其进行解密的 bean。

@Component("decryptor") 
public class DecryptorService {

  public String decrypt(String encryptedString) { 
   ///
  }
}

有没有办法提高我的 XML 以便构造函数参数通过将加密的 属性 传递给此 DecryptorService 来获取其值?

假设您对您这边的代码具有写入权限。 使用如下 @Bean 方法创建 @Configuration class

@Configuration
class ApplicationConfig {
    @Autowired DecryptorService decryptorService;
    @Autowired Properties props;

    @Bean
    public String clearTextPassword() {
        decryptorService.decrypt(props.getEncryptedPassword());
    }
}

然后更改您的 bean 定义以使用 ref

<bean class="com.thirdparty.CoolClass" id="coolClassId">
   <constructor-arg index="1" ref="clearTextPassword" />
</bean>