title: Enabling spring aop sidesteps dependency injection
body: <p>I have a simple spring bean (that of an S2 action class) which uses autowiring to get injected with dependencies working as per usual. However when I subject one of this bean's methods to spring aop, say a <code>@Before</code> advice using JDK/cglib proxy, the dependency injection is skipped leaving my variables unset. </p>
  
  <p>I understand this has something to do with the way spring aop accomplishes join points through proxying, but I can't find anything from the docs about this particular issue. What am I missing?</p>
  
  <p>I can post the context xml too if someone needs to take a look, but it's really very simple in that, all it contains is a couple of beans for the action class, a bean for the aspect and a <code>&lt;aop:aspectj-autoproxy&gt;</code> setting.</p>
  
  <p><strong>Answer:</strong></p>
  
  <p>Here's the mistake:</p>
  
  <pre><code>&lt;action name="sampleAction" class="com.example.s2.SampleAction" method="actionMethod"&gt;
  </code></pre>
  
  <p>The <code>class</code> attribute must have been referring to the spring bean in the spring's app context instead. </p>
  
  <p><strong>Update 1</strong>:</p>
  
  <ol>
  <li>I've tried both the JDK proxying and CGLib, and the same issue occurs in both the cases.</li>
  <li>Using bean properties to inject dependecies (instead of autowiring) didn't help either.</li>
  <li>I use spring 4.0.0.RELEASE and struts2.1.3.</li>
  </ol>
  
  <p><strong>Update 2 (included the code)</strong>:</p>
  
  <pre><code>&lt;!-- ======================== Spring context annotation support======================== --&gt;
      &lt;context:annotation-config /&gt;
      &lt;bean id="testAspect" class="com.example.s2.aop.TestAspect" /&gt;
  
      &lt;!-- ======================== AspectJ configuration======================== --&gt; 
      &lt;!-- Proxy-target-class must be set true if the object being proxied does not implement even a single interface (or if the advised method
             does not come from an interface. Setting this to true tells spring to use cglib for proxying --&gt;
      &lt;aop:aspectj-autoproxy proxy-target-class="true" /&gt;
  
    &lt;!-- ======================== Spring managed beans ======================== --&gt;
      &lt;bean id="sampleActionBean"
              class="com.example.s2.SampleAction"
              scope="prototype"&gt;
      &lt;/bean&gt;
  
      &lt;!-- use a single offer service manager instance throughout the course of the context --&gt;
      &lt;bean id="dependency1" class="com.example.s2.DependencyProviderFactory" factory-method="getDependency1Instance" /&gt;
  </code></pre>
  
  <p>TestAspect class:</p>
  
  <pre><code>@Aspect
  public class TestAspect
  {
    private static final Logger SLOGGER = Logger.getLogger(TestAspect.class);
    @Before ("actionAnnotatedPointCut() &amp;&amp; (publicActionMethodPointCut() || protectedActionMethodPointCut())")
    public void checkUserAccess(JoinPoint pJoinPoint) throws Throwable
    {
      SLOGGER.smartError("Intercepted:", pJoinPoint.getSignature().getName());
    }
  
    @AfterThrowing (value = "actionAnnotatedPointCut() &amp;&amp; (publicActionMethodPointCut() || protectedActionMethodPointCut())", throwing = "pException")
    public void handlePortletException(JoinPoint pJoinPoint, Exception pException)
    {
      SLOGGER.smartError("Method threw error:", pJoinPoint.getSignature().getName(), "| Exception:", pException);
    }
  
    @Pointcut ("@annotation(org.apache.struts2.convention.annotation.Action")
    public void actionAnnotatedPointCut() {}
  
    @Pointcut ("execution(public String *(..))")
    public void publicActionMethodPointCut() {}
  
    @Pointcut ("execution(protected String *(..))")
    public void protectedActionMethodPointCut() {}
  }
  </code></pre>
  
  <p>Class being advised (SampleAction):</p>
  
  <pre><code>public class SampleAction
  {
    @Autowired
    private Dependency1 dependency1;
    ...
  }
  </code></pre>
  
  <p>struts.xml:</p>
  
  <pre><code>&lt;constant name="struts.objectFactory" value="spring" /&gt;
  
  &lt;action name="sampleAction" class="com.example.s2.SampleAction" method="actionMethod"&gt;
    &lt;result name="success"&gt;/pages/actionOutput.jsp&lt;/result&gt;
  &lt;/action&gt;
  </code></pre>
  
  <p><strong>Update 3</strong>:</p>
  
  <p>I think I found the problem, the <code>class</code> of the <code>action</code> in the struts config must be <code>sampleActionBean</code>. I know I started off this way, not sure when I reverted, my apologies.</p>