Results 1 to 9 of 9

Thread: TimerFactoryBean - ClassCastException

  1. #1
    Join Date
    Jun 2005
    Location
    London, uk
    Posts
    25

    Default TimerFactoryBean - ClassCastException

    Hi,

    I am using Spring 1.2.4 with a scheduled task which works (!) but when I try to get a reference to the bean within the code it produces a class cast exception. I have the following configuration:

    <bean id="methodInvokingTask" class="org.springframework.scheduling.timer.Method InvokingTimerTaskFactoryBean">
    <property name="targetObject">
    <ref local="applicationMonitor"/>
    </property>
    <property name="targetMethod">
    <value>queueSize</value>
    </property>
    </bean>

    <bean id="scheduledTask" class="org.springframework.scheduling.timer.Schedu ledTimerTask">
    <property name="delay">
    <value>10000</value>
    </property>
    <property name="period">
    <value>5000</value>
    </property>
    <property name="timerTask">
    <ref local="methodInvokingTask"/>
    </property>
    </bean>

    <bean id="timerScheduler" class="org.springframework.scheduling.timer.TimerF actoryBean">
    <property name="scheduledTimerTasks">
    <list>
    <ref bean="scheduledTask"/>
    </list>
    </property>
    </bean>

    In the code I have the following (which produces the exception):

    org.springframework.scheduling.timer.TimerFactoryB ean timerScheduler = (org.springframework.scheduling.timer.TimerFactory Bean) m_springBeanFactory.getBean("timerScheduler");

    Does anyone know why this is happening ?

    thanks

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

    Default

    This is, because classes implementing FactoryBean (as is in your case) are treated specially by the BeanFactory/ApplicationContext. On retrieval you won't get the FactoryBean, but the bean it produces.
    That said, there is a way to get the FactoryBean itself: You have to prepend the bean-name with an ampersand (e.g. "&timerScheduler"). Maybe you have to xml-escape the ampersand, because it is a special character.

    Regards,
    Andreas

  3. #3
    Join Date
    Jun 2005
    Location
    London, uk
    Posts
    25

    Default

    thanks Andreas, couldn't actually get hold of the bean using the ampersand, but it is academic, as it works and I don't need to get hold of the FactoryBean.

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

    Default

    Fine

    Though you find it academic: With this string you should be able to lookup the factory bean: "& amp;timeScheduler". Just omit the blank between & and amp (seem to be problematic to post xml-entities here).

    Regards,
    Andreas

  5. #5
    Join Date
    Jun 2005
    Location
    London, uk
    Posts
    25

    Default

    yes managed to specify it in the context file, but couldn't find the right lookup string to use in the code:

    These didn't work :

    m_springBeanFactory.getBean("&timerScheduler");
    m_springBeanFactory.getBean("\\&timerScheduler");

    I am probably being really stupid here...

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

    Default

    Quote Originally Posted by djmh68
    m_springBeanFactory.getBean("&timerScheduler");
    This one should do. It is also specified in the reference manual this way.
    What error do you get?

    Regards,
    Andreas

  7. #7
    Join Date
    Jun 2005
    Location
    London, uk
    Posts
    25

    Default

    ok I have this:

    <bean name="&amp;timerScheduler" class="org.springframework.scheduling.timer.TimerF actoryBean">

    and this:

    org.springframework.scheduling.timer.TimerFactoryB ean timerScheduler = (org.springframework.scheduling.timer.TimerFactory Bean) m_springBeanFactory.getBean("&timerScheduler");

    - and I get this error on start up:

    org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'timerScheduler' is defined: org.springframework.beans.factory.xml.XmlBeanFacto ry defining beans [...,currentMemoryScheduledTask,&timerScheduler,app licationMonitor,...]; root of BeanFactory hierarchy
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:349)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedBeanDefinition(AbstractBeanFa ctory.java:671)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:147)

    I am using JDK 1.4.2_04 and JBuilder.

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

    Default

    Ah I see.

    The bean name in the context has to be defined without &.
    Just <bean name="timerScheduler" ... as you had it before.

    Only on accessing the bean (when you need the factory) you have to specify "&timerScheduler".

    getBean("timerScheduler") returns the produced bean.
    getBean("&timerScheduler") returns the factory.

    My remark about the xml-escaping was about referencing (not defining) the bean from within the context itself. Maybe I have been unclear about this.

    Regards,
    Andreas

  9. #9
    Join Date
    Jun 2005
    Location
    London, uk
    Posts
    25

    Default

    yes that works, thanks again

    Dan

Posting Permissions

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