title: AspectJ Pointcut Expression on domain objects not managed by Spring
body: <p><strong>Question:</strong> Can Spring point-cut expressions run on non-managed spring components such as domain object? From my experiments looks like it doesnt, so what is the best way to run pointcut expressions on a regular object?</p>
  
  <p>I created custom annotation name @Encrypt, so that when it is used on top of a field in a domain object, the field is sent to a web service and is automatically encrypted. </p>
  
  <p>I first started with method level annotation, and found that point cut expression doesn't work on Objects not managed by Spring, it has to be a spring bean. </p>
  
  <p><strong>1. Spring Aspect:</strong> Checks for custom annotation @Encrypt and prints out.</p>
  
  <pre><code>@Aspect
  public class EncryptAspect {
  
      @Around("@annotation(encrypt)")
      public Object logAction(ProceedingJoinPoint pjp, Encrypt encrypt)
              throws Throwable {
  
          System.out.println("Only encrypt annotation is running!");
          return pjp.proceed();
      }
  }
  </code></pre>
  
  <p><strong>2. Custom Annotation:</strong> </p>
  
  <pre><code>@Documented
  @Target(ElementType.METHOD)
  @Retention(RetentionPolicy.RUNTIME)
  
  public @interface Encrypt
  {
      // Handled by EncryptFieldAspect
  }
  </code></pre>
  
  <p><strong>3. Domain object using Annotation</strong> </p>
  
  <pre><code>public interface CustomerBo {
      void addCustomerAround(String name);
  }
  
  public class CustomerBoImpl implements CustomerBo {    
      @Encrypt
      public void addCustomerAround(String name){
          System.out.println("addCustomerAround() is running, args : " + name);
      }
  }
  </code></pre>
  
  <p><strong>4. Invocation</strong></p>
  
  <pre><code>        ApplicationContext appContext = new ClassPathXmlApplicationContext("http-outbound-config.xml");
  //      CustomerBoImpl customer = new CustomerBoImpl(); --&gt; Aspect is not fired if object is created like this.
          CustomerBo customer = (CustomerBo) appContext.getBean("customerBo"); // Aspect Works
          customer.addCustomerAround("test");
  </code></pre>