Announcement Announcement Module
No announcement yet.
TransactionProxyFactoryBean and getBean() problem Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 :


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

    <!-- 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 id="accountManager" parent="txProxyTemplate">
    <property name="target">
    <bean class="com.planaxis.swifter.service.impl.AccountMa nagerImpl" autowire="byName"/>

    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"/>

    <!-- 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>

    <!-- 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">

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


    <!-- 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 id="accountDAO" class="com.planaxis.swifter.dao.hibernate.AccountD AOHibernate" autowire="byName">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    A piece of code :
    AccountManagerImpl brutManager = (AccountManagerImpl )
    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:
    AccountManagerImpl brutManager = &#40;AccountManagerImpl &#41; 
    You're trying to cast to AccountManagerImpl. If there's an AccountManager interface you should be able to cast to that.

    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.



    • #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)
      This throws a classCastException ...
      I don't understand what is wrong