title: ClassCastException $Proxy cannot be cast to using aop
body: <p>I was using spring to create objects through beans. Now I tried to use aop to create the same object and I get $Proxy cannot be cast to SaleRoom exception.</p>
  
  <p>the previous xml was:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
  xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
  xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config- 1.0.xsd"
  xmlns:jm s="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
  xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
  xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
  xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
  xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
  xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
  xmlns:p="http://www.springframework.org/schema/p"
  
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-2.5.xsd
  http://www.springframework.org/schema/context/spring-context-2.5.xsd     http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-2.5.xsd
  http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd     http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-2.5.xsd
  http://www.springframework.org/schema/jms/spring-jms-2.5.xsd     http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-2.5.xsd
  http://www.springframework.org/schema/jee/spring-jee-2.5.xsd     http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-2.5.xsd
  http://www.springframework.org/schema/lang/spring-lang-2.5.xsd     http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-2.5.xsd
  http://www.springframework.org/schema/osgi/spring-osgi.xsd     http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-2.5.xsd
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd     http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-2.5.xsd
  http://www.springframework.org/schema/util/spring-util-2.5.xsd     http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-2.5.xsd
  "&gt;
  &lt;bean id="sale01" class="application.common.entities.BidRoom"&gt;
  &lt;property name="itemId" value="0001"/&gt;
  &lt;property name="lifeTime" value="15"/&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>And I used the following code to create the sales:</p>
  
  <pre><code>    ApplicationContext context = new FileSystemXmlApplicationContext(SalesManager.getSalesSourceFile());
      SaleRoom saleRoom;
      List&lt;String&gt; salesNames = new LinkedList&lt;String&gt;();
      List&lt;SaleRoom&gt; allSales = new LinkedList&lt;SaleRoom&gt;();
  
      // Get all sales id's for beans
      NodeList salesNodeList = salesDoc.getElementsByTagName("bean");
  
      for (int i = 0; i &lt; salesNodeList.getLength(); i++) {
          Node nNode = salesNodeList.item(i);
          salesNames.add(((Element) nNode).getAttribute("id").toString());
      }
  
      for (String saleName : salesNames) {
          if(saleName.contains("sale")) {
              saleRoom = (SaleRoom) context.getBean(saleName);
              allSales.add(saleRoom);
          }
      }
  
      return allSales;
  </code></pre>
  
  <p>This is the new xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"&gt;
  
      &lt;aop:aspectj-autoproxy&gt;
          &lt;aop:include name="logSettersCalls"/&gt;
      &lt;/aop:aspectj-autoproxy&gt;
      &lt;bean id="logSettersCalls" class="application.logging.aop.LogSettersCalls"/&gt;
  
      &lt;bean id="sale01" class="application.common.entities.BidRoom"&gt;
          &lt;constructor-arg index="0" type="int" value="0001"/&gt;
          &lt;constructor-arg index="1" type="int" value="15"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>The Aspect logging class:</p>
  
  <pre><code>@Aspect
  public class LogSettersCalls {
     @Pointcut("execution(void set*(*))")
      public void setMethod() {}
  
      @Before("setMethod()")
      public void logSetterCall(JoinPoint theJoinPoint) {
          String methodName = theJoinPoint.getSignature().getName();
          Object newValue = theJoinPoint.getArgs()[0];
          Object theObject = theJoinPoint.getTarget();
          System.out.println("The method " + methodName + " is called on object " 
                  + theObject + " with the value " + newValue);
      }
  }
  </code></pre>
  
  <p>I'm using the same code for creating the beans via aop. and I get 
  Exception in thread "main" java.lang.ClassCastException: $Proxy11 cannot be cast to application.common.entities.SaleRoom</p>
  
  <p>The line that throws the exception: 
  saleRoom = (SaleRoom) context.getBean(saleName);</p>
  
  <p>Any help will be appreciated. Thanks. </p>