私の最初の努力は貧弱でしたが、急いでいました。謝罪します。私はあなたが望むものを自分で実装したと信じているので、それがどのように機能するべきかを知っていると思います.
Credential クラスから始めました (注: インターフェイスなし):
package aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Credential
{
private static final String DEFAULT_USERNAME = "username";
private static final String DEFAULT_PASSWORD = "password";
private String username;
private String password;
public static void main(String[] args)
{
Credential cred1 = new Credential("foo", "bar");
System.out.println("created using new: " + cred1);
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:aop-context.xml");
Credential cred2 = (Credential) context.getBean("credential");
System.out.println("created using app context: " + cred2);
String password = ((args.length > 0) ? args[0] : "baz");
cred2.setPassword(password);
System.out.println("initialized using setter: " + cred2);
}
public Credential()
{
this(DEFAULT_USERNAME, DEFAULT_PASSWORD);
}
public Credential(String username, String password)
{
this.setUsername(username);
this.setPassword(password);
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String toString()
{
return new StringBuilder().append("Credential{").append("username='").append(username).append('\'').append(", password='").append(password).append('\'').append('}').toString();
}
}
Decryptor インターフェイスを作成しました。
package aop;
public interface Decryptor
{
String decrypt(String encrypted);
}
そして DecryptorImpl:
package aop;
public class DecryptorImpl implements Decryptor
{
public static final String DEFAULT_DECRYPTED_VALUE = " - not secret anymore";
public String decrypt(String encrypted)
{
// Any transform will do; this suffices to demonstrate
return encrypted + DEFAULT_DECRYPTED_VALUE;
}
}
Spring の MethodBeforeAdvice を実装するには、DecryptorAdvice が必要でした。
package aop;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class DecryptionAdvice implements MethodBeforeAdvice
{
private Decryptor decryptor;
public DecryptionAdvice(Decryptor decryptor)
{
this.decryptor = decryptor;
}
public void before(Method method, Object[] args, Object target) throws Throwable
{
String encryptedPassword = (String) args[0];
args[0] = this.decryptor.decrypt(encryptedPassword);
}
}
そして、それを aop-context.xml にまとめました。(XML を表示する方法を教えていただければ、投稿します。) passwordDecryptionAdvisor に注意してください。setPassword メソッドにのみ一致します。
興味深い部分は、実行すると発生します。コンソールに表示される内容は次のとおりです。
created using new: Credential{username='foo', password='bar'}
created using app context: Credential{username='stackoverflow', password='encrypted-password'}
initialized using setter: Credential{username='stackoverflow', password='baz - not secret anymore'}
これが私に伝えていることは次のとおりです。
- new でオブジェクトを作成すると、Spring の制御下にないため、アドバイスは適用されません。
- アプリ コンテキストが初期化される前に ctor で setPassword を呼び出すと、アドバイスは適用されません。
- アプリ コンテキストの初期化後にコードで setPassword を呼び出すと、アドバイスが適用されます。
これがお役に立てば幸いです。