我有一个 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/