Announcement Announcement Module
Collapse
No announcement yet.
HibernateDaoSupport - BeanNameAutoProxyCreator - ClassCast Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateDaoSupport - BeanNameAutoProxyCreator - ClassCast Exception

    I am trying a simple logging interceptor for a bean which extends HibernateDaoSupport..When i try to do getBean and cast it to my bean, i get java.lang.ClassCastException: $Proxy4..

    if i remove extends from HibernateDaoSupport, aspect with BeanNameAutoProxyCreator works fine and if remove BeanNameAutoProxyCreator aspect, just the dao and hibernate works fine..it only fails if i extend hibernatedaosupport and apply beannameautoproxy aspect..

    1)below is my java file
    Code:
    package springex;
    import net.sf.hibernate.SessionFactory;
    /**
     * @author administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    
    
    public class AdderImpl extends org.springframework.orm.hibernate3.support.HibernateDaoSupport
    {
    	//private SessionFactory sessionFactory;
        public int add(int arg0, int arg1)
        {
            return arg0 + arg1;
        }
    
        public void add(){
    	}
    
    }
    2)below is my spring config
    Code:
    <beans>	
     
     	 <bean id="appDataSource" class="org.apache.commons.dbcp.BasicDataSource">
      		<property name="driverClassName"><value>COM.ibm.db2.jdbc.app.DB2Driver</value></property>
      	        <property name="url"><value>jdbc:db2:dbname</value></property>
      		<property name="username"><value>userid</value></property>
      		<property name="password"><value>password</value></property>
      	</bean> 
      
      	<bean id="appHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      		<property name="properties">
      			<props>
      				<prop key="hibernate.hbm2ddl.auto">update</prop>
      				<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
      				<prop key="hibernate.query.substitutions">true 'T', false 'F'</prop>
      				<prop key="hibernate.show_sql">true</prop>
      				<prop key="hibernate.c3p0.minPoolSize">5</prop>
      				<prop key="hibernate.c3p0.maxPoolSize">20</prop>
      				<prop key="hibernate.c3p0.timeout">600</prop>
      				<prop key="hibernate.c3p0.max_statement">50</prop>
      				<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
      			</props>
      		</property>
    	</bean>
    	
    	<bean id="appSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="appDataSource"/></property>
    		<property name="hibernateProperties">
    			<ref bean="appHibernateProperties" />
    		</property>
    			
    			
    		<property name="mappingResources">
    			<list>
    			         <value>Mapping.hbm.xml</value>
    		   	</list>
    		</property>
    			
    			
    	</bean>
      	 <bean id="Adder1" class="springex.AdderImpl">
    	  	<property name="sessionFactory"><ref local="appSessionFactory"/></property>
    	  </bean>	
      <bean id="loggingInterceptor" class="springex.LoggingInterceptor"/>
      <bean id="loggingProxy"
           class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
           <property name="beanNames">
             <list>
               <value>*</value>
             </list>
           </property>
           <property name="interceptorNames">
             <list>
               <value>loggingInterceptor</value>
             </list>
           </property>
        </bean>
    3)below is my class that invokes..

    Code:
    public static void main(String[] args)
        {
        	ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {
            "springbeans.xml"   });
    
    System.out.println ("class :: " + appContext.getBean("Adder1"));
            AdderImpl a = (AdderImpl) appContext.getBean("Adder1");
            System.out.println("result = " + a.add(4, 7));
    }
    4)below is my classpath
    Code:
    db2java.zip;
    commons-dbcp.jar;
    commons-pool.jar;
    db-ojb-1.0.4.jar;
    hibernate2.jar;
    spring.jar; (1.2.8)
    spring-aop.jar;
    commons-logging-api.jar;
    jta.jar;
    dom4j-1.5.2.jar;
    commons-collections-3.0.jar;
    ehcache-1.1.jar;
    hibernate3.jar;
    cglib-nodep-2.1_3.jar

  • #2
    Best practice kinda dictates that you build to interfaces, so you should really have an Addr interface which you can then cast to.

    Comment


    • #3
      it works fine if i have an interface...i have existing project which has developed classes...so i wanted to use BeanNameAutoProxyCreator so that i dont need to define interfaces for all these classes..
      Last edited by aravindbv; Aug 25th, 2006, 12:45 PM.

      Comment


      • #4
        Can you try setting the proxyTargetClass property of the BeanNameAutoProxyCreator to the value 'true' (without the quotes)?

        Comment


        • #5
          thanks that works..if i give * for beanNames values then i get exception as below...If i give Add*, it works fine without interface...

          Code:
          <bean id="loggingProxy"
                 class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
                 <property name="beanNames">
                   <list>
                     <value>*</value>
                   </list>
                 </property>
                 <property name="interceptorNames">
                   <list>
                     <value>loggingInterceptor</value>
                   </list>
                 </property>
              </bean>
          Exception

          Code:
          Exception in thread "main" org.springframework.beans.factory.FactoryBeanNotIniti
          alizedException: Error creating bean with name 'appHibernateProperties': Factory
          Bean returned null object: probably not fully initialized (maybe due to circular
           bean reference)

          Comment


          • #6
            Yes a * would affect all beans defined in the app context.

            Comment


            • #7
              thanks for the reply...will there be performance hit if i use BeanNameAutoProxyCreator with <property name="proxyTargetClass" value="true" />..or do i need to define interfaces for all classes that dont have it and not use proxyargeClass to true for better performance..

              Comment


              • #8
                Originally posted by aravindbv
                thanks for the reply...will there be performance hit if i use BeanNameAutoProxyCreator with <property name="proxyTargetClass" value="true" />..or do i need to define interfaces for all classes that dont have it and not use proxyargeClass to true for better performance..
                There is a slight performance difference between JDK dynamic proxying and CGLIB proxying. And this difference fluctuates with each JDK release. So the exact loss/gain would depend on your JDK version. I have not noticed any "visible" performance losses myself but you may want to test it out for your application.

                Comment


                • #9
                  I did some stupidly trivial performance tests on my old blog (http://blogs.warwick.ac.uk/colinyates/) which essentially created 10000 instances of a class and then performed 10000 calls on a method.

                  It timed this for a class, a class extended by CGLIB and a class wrapped in a JDK proxy. The stats were that the 10000x10000 native calls took something like 37ms, the CGLIB took 200ms and the JDK (1.5_05) proxy took 700ms. I did the same thing on the latest JDK (1.5_07) and the proxy calls took about the same time as the CGLIB.

                  I cannot link to the original article because the blog system is currently down

                  Comment


                  • #10
                    import net.sf.hibernate.SessionFactory;
                    ...
                    org.springframework.orm.hibernate3.support.Hiberna teDaoSupport
                    You are trying to use Hib2 SessionFactory and Hib3 Dao Support.

                    Comment

                    Working...
                    X