Announcement Announcement Module
Collapse
No announcement yet.
ClassCastException when retrieving bean from ApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException when retrieving bean from ApplicationContext

    Hi Folks,


    I've got two JUnit TestCases set up to test a configuration that is using spring/ibatis to access a database. One is working and the other is throwing a ClassCastException and I'm getting a $Proxy5 back.

    To consolidate the setup, I've created an abstract base class that creates the ApplicationContext in its setUp() method, which then delegates to an abstract method that is defined in the two subclasses.

    First here is the code (the configuration will follow):

    Code:
     
    public abstract class SqlMapDaoBaseTest extends TestCase {  
        private static appConfigXml = "/path/to/config/xml.xml"; 
        protected void setUp() throws Exception {
            ctx = new FileSystemXmlApplicationContext(appConfigXml);
            Resource resource = ctx.getResource(appConfigXml);
            appFactory = new XmlBeanFactory(resource);
            initDao(ctx); 
        }
        
        protected abstract void initDao(ApplicationContext ctx);
    }
    
    public class SqlMapEmpDaoTest extends SqlMapDaoBaseTest {
        private EmpDao dao;
        
        protected void initDao(ApplicationContext ctx) {
            dao = (EmpDao) ctx.getBean("empDao");
        }
    }
    
    public class SqlMapDepDaoTest extends SqlMapDaoBaseTest {
        private DepDao dao;
        
        protected void initDao(ApplicationContext ctx) {
            dao = (DepDao) ctx.getBean("depDao");
        }
    }
    here is the configuration:

    Code:
      <bean id="baseProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="transactionAttributes">
          <props>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="update*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
        </property>
      </bean>
    
      <!-- SqlMapClient setup for iBATIS Database Layer -->
      <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="path/to/ibatis/sqlMapConfig.xml"/>
      </bean>
    
      <bean id="empDao" parent="baseProxy">
        <property name="target">
          <bean class="com.foo.dao.ibatis.SqlMapEmpDao">
            <property name="dataSource" ref="dataSource"/>
            <property name="sqlMapClient" ref="sqlMapClient"/>
          </bean>
        </property>
      </bean>
    
      <bean id="depDao" parent="baseProxy">
        <property name="target">
          <bean class="com.foo.dao.ibatis.SqlMapDepDao">
            <property name="dataSource" ref="dataSource"/>
            <property name="sqlMapClient" ref="sqlMapClient"/>
          </bean>
        </property>
      </bean>

    I think I've included all relevent information. As far as I can tell, everything is
    nearly identical, except where classes and bean ids are different. The test in SqlMapEmpDaoTest works fine but the exception gets thrown when I try to get the bean in the single call found in SqlMapDepDaoTest.initDao()

    Code:
    java.lang.ClassCastException: $Proxy5
    	at com.foo.dao.ibatis.SqlMapDepDaoTest.initDao(SqlMapDepDaoTest.java:32)
    	at com.foo.dao.ibatis.SqlMapDaoBaseTest.setUp(SqlMapDaoBaseTest.java:36)
    Any Ideas? Maybe I'm just not seeing something but I'm all out of ideas myself.

    Thanks,
    -gt
Working...
X