我正在尝试使用别名来做与上述帖子类似的事情,以便使用属性交换 bean 引用(字面意思与上述帖子的目的相同)。
但是,当我尝试它时,我得到了原因:java.lang.IllegalArgumentException:无法解析占位符'crypto.provider'
如果我使用委托将相同的占位符作为构造函数参数传递,它可以正常工作。下面是代码
#property file
crypto.provider = safenet
应用程序上下文.xml
<beans>
<alias name="${crypto.provider}" alias="defaultProvider" />
</beans>
类:
@Component ("caesar")
public class CaesarCipherEncryptionProvider implements EncryptionProvider{
//implementation here
}
@Component ("safenet")
public class SafenetEncryptionProvider implements EncryptionProvider{
//implementation here
}
测试用例
public class SafeNetEncryptionProviderTest extends AbstractIntegrationTest {
@Autowired
@Qualifier("defaultProvider") // note: Intellij can't find this bean
private EncryptionProvider provider;
@Test
public void testEncryptDecrypt() throws Exception{
String plaintext = "every.other.email.address.is.shorter.than.this.email.address" +
"@long-email-addresses-incorporated.home-of-the-extra-long-email-addresses.com" +
".just.kidding.this.domain.is.really.German.de";
byte[] plainTextAsBytes = plaintext.getBytes("UTF-8");
byte[] encrypted = provider.encrypt(plaintext);
String decrypted = provider.decrypt(encrypted);
assertFalse(Arrays.equals(plainTextAsBytes, encrypted));
assertEquals(plaintext, decrypted);
}
}