Announcement Announcement Module
Collapse
No announcement yet.
TransactionProxyFactoryBean and getBean() problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • TransactionProxyFactoryBean and getBean() problem

    Hi, i'm a newbie in spring
    I've a problem with TransactionProxyFactoryBean and the getBean() function.
    When i want to get a reference to the bean, this throws a ClassCastException.

    Here is my Service Context :


    <beans>


    <bean id="txProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>

    <!-- Generic manager that can be used to do basic CRUD operations on any objects -->
    <bean id="manager" parent="txProxyTemplate">
    <property name="target">
    <bean class="com.planaxis.swifter.service.impl.BaseManag er">
    <property name="DAO"><ref bean="dao"/></property>
    </bean>
    </property>
    </bean>




    <!--Account-START-->
    <bean id="accountManager" parent="txProxyTemplate">
    <property name="target">
    <bean class="com.planaxis.swifter.service.impl.AccountMa nagerImpl" autowire="byName"/>
    </property>
    </bean>
    <!--Account-END-->

    Here is my dao Context

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521lanaxis"/>
    <property name="username" value="testweb"/>
    <property name="password" value="testweb"/>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager" >
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>


    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean" >
    <property name="dataSource"><ref bean="dataSource"/></property>

    <!-- Must references all OR mapping files. -->
    <property name="mappingResources">
    <list>
    <value>com/planaxis/swifter/model/Account.hbm.xml</value>
    </list>
    </property>

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Orac le9Dialect</prop>
    </props>
    </property>

    </bean>

    <!-- Generic DAO - can be used when doing standard CRUD -->
    <bean id="dao" class="com.planaxis.swifter.dao.hibernate.BaseDAOH ibernate">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>



    <bean id="accountDAO" class="com.planaxis.swifter.dao.hibernate.AccountD AOHibernate" autowire="byName">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    A piece of code :
    AccountManagerImpl brutManager = (AccountManagerImpl )
    appContext.getBean("accountManager");
    But this line throw :
    java.lang.ClassCastException: $Proxy8
    [java] at com.planaxis.swifter.front.Main.launch(Unknown Source)

    What is the problem, don't undertsand

  • #2
    Unless you set the proxyTargetClass flag to true (default is false), Spring will use a dynamic proxy. Dynamic proxies can only proxy interfaces. Thus there really is a class cast exception:
    Code:
    AccountManagerImpl brutManager = &#40;AccountManagerImpl &#41; 
    appContext.getBean&#40;"accountManager"&#41;;
    You're trying to cast to AccountManagerImpl. If there's an AccountManager interface you should be able to cast to that.

    Solutions:
    1. Cast to the interface and code against the interface, not the class. This is best practice, anyway.
    2. Use the optional proxyTargetClass flag on your tx proxy.

    Rgds
    Rod

    Comment


    • #3
      Yes but i've already a interface AccountManager (Of course this is better than cast to the implementation )and when i wrote this :

      AccountManager brutManager = (AccountManager)
      appContext.getBean("accountManager");
      This throws a classCastException ...
      I don't understand what is wrong

      Comment

      Working...
      X