title: Performance problems when using lots of AOP request scoped beans
body: <p>I'm working on a semi-large application using Spring 3 and am running into performance problems when throwing hundreds of users at it at once.  I'm using several request scoped beans using Spring's AOP proxy and I can see that every time I call any method on one of these beans, the CGLIB interceptor is invoked, which then calls AbstractBeanFactory.getBean(), which calls add() on a Synchronized Set of existing Spring beans.  Since this add() is synchronized, it effectively locks up the server when there are thousands of calls to it all waiting to add to the same list.  </p>
  
  <p>Is there a way to get around this using request scoped beans?  I read in the Spring documentation that CGLIB isn't used if the bean implements any interface (http://static.springsource.org/spring/docs/2.0.0/reference/aop.html#d0e9015) but my request scoped beans all implement one (the same one in fact) and it's still happening.  And I definitely need the beans to be request scoped because some of their fields are calculated in one part of the app for a particular request and then I use SpEL to get their value in a different part of the app during the same request.  I think if I made the beans prototype scoped, I'd have a fresh object when I used SpEL to get them the second time.</p>
  
  <p>Here is a code sample that illustrates my problem.  See the last two lines for comments describing where exactly I'm having issues.</p>
  
  <pre><code>&lt;!-- Spring config --&gt;
  &lt;bean name="someBean" class="some.custom.class.SomeClass" scope="request"&gt;
      &lt;property name="property1" value="value1"/&gt;
      &lt;property name="property2" value="value2"/&gt;
      &lt;aop:scoped-proxy/&gt;
  &lt;/bean&gt;
  
  &lt;bean name="executingClass" class="some.other.custom.class.ExecutingClass" scope="singleton"&gt;
      &lt;property name="myBean" ref="someBean" /&gt;
  &lt;/bean&gt;
  
  
  public Interface SomeInterface {
      public String getProperty1();
      public void setProperty1(String property);
      public String getProperty2();
      public void setProperty2(String property);
  }
  
  public class SomeClass implements SomeInterface {
      private String property1;
      private String property2;
  
      public String getProperty1() { return propery1; }
      public void setProperty1(String property) { property1=property;}
  
      public String getProperty2() { return propery2; }
      public void setProperty2(String property) { property2=property;}
  }
  
  
  public class ExecutingClass {
      private SomeInterface myBean;
  
      public void execute() {
          String property = myBean.getProperty1(); // CGLIB interceptor is invoked here, registering myBean as a bean
          String otherProperty = myBean.getProperty2(); // CGLIB interceptor is invoked here too!  Seems like this is unnecessary. And it's killing my app.
      }
  }
  </code></pre>
  
  <hr>
  
  <p>My ideas are one of the following:</p>
  
  <ul>
  <li>Can I make a Spring Bean request scoped without proxying every method call made on the bean? And without marking every method as "final"?</li>
  </ul>
  
  <p>or...</p>
  
  <ul>
  <li>Can I override Spring's bean factory to implement a Bean cache that will check if a bean is cached before calling AbstractBeanFactory.getBean()?  And if so, where do I configure Spring to use my custom bean factory?</li>
  </ul>