gpt4 book ai didi

java - 将动态值注入(inject) bean 的 constructor-arg 标记

转载 作者:行者123 更新时间:2023-11-30 10:18:40 25 4
gpt4 key购买 nike

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

<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) {
///
}
}

是否可以通过将加密属性传递给此 DecryptorService 来提升我的 XML,以便构造函数参数获取其值?

最佳答案

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

@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>

关于java - 将动态值注入(inject) bean 的 constructor-arg 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49142975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com