Results 1 to 9 of 9

Thread: java.lang.ClassCastException: $Proxy1

  1. #1
    Join Date
    May 2005
    Location
    Canada
    Posts
    6

    Default java.lang.ClassCastException: $Proxy1

    Hi,

    I am new to Spring and am having difficulties with the applicationContext-hibernate.xml file. When I setup 2 bean objects everything works fine, but when I add a third one, the HashMap definitions for the XmlWebApplicationContext beanFactory's beanDefinitionMap get all mixed-up with certain entries being missing. I was able to variate the problem somehow by changing the names of the services and targets in the definitions which leads me to believe that the issue is with the unique id's being hashed incorrectly. I am including the relevant section from the configuration file for reference.

    Code:
    <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    	<bean id="someTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="someSessionFactory"/></property>
    	</bean>
    	
    
    	<!-- ***** LOGIN SERVICE ***** -->
    	<bean id="loginService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">	
    		<property name="transactionManager"><ref local="someTransactionManager"/></property>
    		<property name="target"><ref local="userLoginTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly,-LoginException</prop>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly,-LoginException</prop>
    				<prop key="save*">PROPAGATION_REQUIRED,-LoginException</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly,-LoginException</prop>
    			</props>
    		</property>
    	</bean>
    
    	<!-- ***** LOOKUP SERVICE ***** -->
    	<bean id="lookupTableService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager"><ref local="someTransactionManager"/></property>
    		<property name="target"><ref local="lookupTableTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly,-RecordNotFoundException</prop>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly,-RecordNotFoundException</prop>
    				<prop key="save*">PROPAGATION_REQUIRED,-DatabaseException</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly,-DatabaseException</prop>
    			</props>
    		</property>
    	</bean>
    
    	<!-- ***** MEMBERSHIP SERVICE ***** -->
    	<bean id="memberService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">	
    		<property name="transactionManager"><ref local="someTransactionManager"/></property>
    		<property name="target"><ref local="memberTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="find*">PROPAGATION_REQUIRED,readOnly,-LoginException</prop>
    				<prop key="get*">PROPAGATION_REQUIRED,readOnly,-LoginException</prop>
    				<prop key="save*">PROPAGATION_REQUIRED,-LoginException</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly,-LoginException</prop>
    			</props>
    		</property>
    	</bean>	
    
    	<!-- LoginTarget primary business object implementation -->
    	<bean id="userLoginTarget" class="com.name.service.spring.LoginServiceSpringImpl">
    		<property name="userDAO"><ref local="userDAO"/></property>
    	</bean>
    
    	<!-- LookupTarget primary business object implementation -->
    	<bean id="lookupTableTarget" class="com.name.service.spring.LookupServiceSpringImpl">
    		<property name="lookupTableDAO"><ref local="lookupTableDAO"/></property>
    	</bean>
    
    	<!-- MemberTarget primary business object implementation -->
    	<bean id="memberTarget" class="com.name.service.spring.MemberServiceSpringImpl">
    		<property name="memberDAO"><ref local="memberDAO"/></property>
    	</bean>
    	
    	<!-- DAO object&#58; Hibernate implementation -->
    	<bean id="userDAO" class="com.name.hibernate.dao.UserHibernateDAO">
    		<property name="sessionFactory"><ref local="someSessionFactory"/></property>		
    	</bean>
    	<!-- DAO object&#58; Hibernate implementation -->
    	<bean id="lookupTableDAO" class="com.name.hibernate.dao.LookupHibernateDAO">
    		<property name="sessionFactory"><ref local="someSessionFactory"/></property>
    	</bean>
    	<bean id="memberDAO" class="com.name.hibernate.dao.MemberHibernateDAO">
    		<property name="sessionFactory"><ref local="someSessionFactory"/></property>
    	</bean>
    All help is greatly appreciated.
    Thanks in advance,
    - Paul

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Can you post the relevant part of the stack trace?

  3. #3
    Join Date
    May 2005
    Location
    Canada
    Posts
    6

    Default

    Here is the stack trace for the indicated problem.

    Code:
    java.lang.ClassCastException&#58; $Proxy1
    	at com.name.action.BaseAction.setServlet&#40;BaseAction.java&#58;36&#41;
    	at org.apache.struts.action.RequestProcessor.processActionCreate&#40;RequestProcessor.java&#58;341&#41;
    	at org.apache.struts.action.RequestProcessor.process&#40;RequestProcessor.java&#58;268&#41;
    	at org.apache.struts.action.ActionServlet.process&#40;ActionServlet.java&#58;1482&#41;
    	at org.apache.struts.action.ActionServlet.doPost&#40;ActionServlet.java&#58;525&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;709&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;802&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;237&#41;
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
    	at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;214&#41;
    	at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;178&#41;
    	at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;126&#41;
    	at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;105&#41;
    	at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;107&#41;
    	at org.apache.catalina.connector.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;148&#41;
    	at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;825&#41;
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;731&#41;
    	at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket&#40;PoolTcpEndpoint.java&#58;524&#41;
    	at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt&#40;LeaderFollowerWorkerThread.java&#58;80&#41;
    	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;684&#41;
    	at java.lang.Thread.run&#40;Thread.java&#58;595&#41;
    and the code in the BaseActioin class that causes it is:

    Code:
    public class BaseAction extends Action
    &#123;
      private ILoginService loginService;
      private IMemberLookupService memberService;
      private ILookupService lookupService;
      
      public void setServlet&#40;ActionServlet actionServlet&#41;
      &#123;
        super.setServlet&#40;actionServlet&#41;;    
        ServletContext servletContext = actionServlet.getServletContext&#40;&#41;;
        WebApplicationContext wac =
          WebApplicationContextUtils.getRequiredWebApplicationContext&#40;servletContext&#41;;
        this.loginService = &#40;ILoginService&#41; wac.getBean&#40;"loginService"&#41;;
        this.memberService = &#40;IMemberLookupService&#41; wac.getBean&#40;"memberService"&#41;;
        this.lookupService = &#40;ILookupService&#41; wac.getBean&#40;"lookupTableService"&#41;;
        System.out.println&#40;"here"&#41;;
      &#125;
      
      /**
       * Login record service.
       * 
       * @return login record service
       */
      public ILoginService getLoginService&#40;&#41;
      &#123;
        return loginService;
      &#125;
      
      /**
       * Member record service.
       * 
       * @return member record service
       */
      public IMemberLookupService getMemberService&#40;&#41;
      &#123;
        return memberService;
      &#125;
      
      /**
       * Lookup table service.
       * 
       * @return lookup table service
       */
      public ILookupService getLookupService&#40;&#41;
      &#123;
        return lookupService;
      &#125;
    &#125;
    - Paul

  4. #4
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    The problem is most probably in your service implementation classes definition, you will need to post them too.

  5. #5
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    In your TransactionProxy definition you do not specify the "proxyInterfaces" attribute. This means, the generated proxies do not implement the interfaces you expect. So you should specify these interfaces to make your code work.

    An additional hint: you might add the class parameter to your invocation of the getBean() method. So spring itself could do type checking, leading to a somewhat better exception message.

    Why this issue should depend on the number of beans available I cannot conclude from the information available.

    Regards,
    Andreas

  6. #6
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    An addition: Cglib is used for proxying (in the absence of an interface specification). Maybe the names of the generated proxy classes pose some problem.

    If you could create a minimal testcase that reproduces this behaviour you should open a JIRA issue.

    Regards,
    Andreas

  7. #7
    Join Date
    May 2005
    Location
    Canada
    Posts
    6

    Default

    Thanks Andreas. The proxyInterfaces specification fixed the problem. I believe that this indicates that the default CGLIB default class must be at fault causing multiple beans to be seen as equal objects; and therefore, the beans are hashed to the same values in the XmlWebApplicationContext beanFactory's beanDefinitionMap.

    - Paul

  8. #8
    Join Date
    May 2005
    Location
    Canada
    Posts
    6

    Default false alarm

    The proxyInterfaces specification was only a trick around. When I added additional items to the applicationContext-hibernate.xml file other things were lost again. Something very strange is going on with the allocation/storage of the items in memory. I am fairly new to this and all help is very appreciated.

    Thanks in advance again,
    Paul

  9. #9
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    I would advise you to create a minimal example that reproduces the problem and open an issue in JIRA
    After creating an issue, you can attach your code to it. I guess, someone will have a look at it there, soon.

    Regards,
    Andreas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •