title: How to @Autowire a bean which is hidden behind ProxyFactoryBean?
body: <p>Let's say we have two beans, defined in Spring</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean class="foo.A"/&gt;
  &lt;bean class="foo.B"/&gt;
  </code></pre>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public class A {
     @Autowired
     private B b;
  }
  
  public class B {
     public void foo() {
        ...
     }
  }
  </code></pre>
  
  <p>What I want to achieve is the interception of all calls to <code>B.foo()</code>. Looking at documentation, I wrote interceptor <code>C</code> and changed the definition of bean <code>B</code> as follows:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public class C implements org.springframework.aop.MethodBeforeAdvice {
      public void before(final Method method, final Object[] args, final Object target) {
          // interception logic goes here
      }
  }
  </code></pre>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean class="foo.C"/&gt;
  
  &lt;bean class="org.springframework.aop.framework.ProxyFactoryBean" scope="prototype"&gt;
      &lt;property name="proxyTargetClass" value="true"/&gt;
      &lt;property name="singleton" value="false"/&gt;
      &lt;property name="target"&gt;
          &lt;bean class="foo.B" scope="prototype"/&gt;
      &lt;/property&gt;
      &lt;property name="interceptorNames"&gt;
          &lt;list&gt;
              &lt;value&gt;foo.C&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>Problem:</strong> when starting up, Spring container complains: <em>No matching bean of type [foo.B] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency</em>. In other words, it  can't inject <code>B</code> into <code>A</code> because <code>B</code> is hidden behind <em>org.springframework.aop.framework.ProxyFactoryBean</em> and no longer "automagically" recognized. If I replace the definition into a simple <code>class=foo.B</code>, container starts fine. What's the best way to solve this?</p>
  
  <p><strong>Bonus question:</strong> is it possible to implement interception of <code>B.foo()</code> without involvement of <code>ProxyFactoryBean</code> and only using annotations (preferably without involvement of <code>&lt;aop:...</code>)?</p>