Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My first effort was poor, but I was in hurry. I apologize. Now I think I know how it should work, because I believe I've implemented what you want myself. </p> <p>I started with a Credential class (note: no interface): </p> <pre><code> 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(); } } </code></pre> <p>I created a Decryptor interface: </p> <pre><code> package aop; public interface Decryptor { String decrypt(String encrypted); } </code></pre> <p>And a DecryptorImpl: </p> <pre><code> 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; } } </code></pre> <p><br/></p> <p> I needed DecryptorAdvice to implement Spring's MethodBeforeAdvice: </p> <p><br/></p> <pre><code> 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); } } </code></pre> <p>And I wired it together in an aop-context.xml. (If you tell me how to get XML to display, I'll post it.) Note the passwordDecryptionAdvisor: it only matches the setPassword method.</p> <p>The interesting part happens when I run it. Here's what I see in the console: </p> <pre><code> 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'} </code></pre> <p>What this tells me is:</p> <ol> <li>If I create an object with new it's not under Spring's control, advice isn't applied.</li> <li>If I call setPassword in the ctor before the app context is initialized, advice isn't applied.</li> <li>If I call setPassword in my code after the app context is initialized, advice is applied.</li> </ol> <p>I hope this can help you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload