Announcement Announcement Module
No announcement yet.
Hmm, ApplicationListener not called for anonymous beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hmm, ApplicationListener not called for anonymous beans

    I just found a funny, and am not sure if it is a bug or not.

    Basically, anonymous "nested" beans do not get onApplicationEvent called. If you pull the same bean out into "root" scope in the XML file and give it a name/id it gets the event called:

    <bean id="X" class="someClass1">
       <property name="beanRef">
            <bean class="someClass2"/>
    If someClass2 implements ApplicationListener the event does not get called. But if I just change the XML to this:

    <bean id="Y" class="someClass2"/>
    <bean id="X" class="someClass1">
       <property name="beanRef" ref="Y"/>
    The above calls the event handler. Unless there is some hidden wisdom here it smells like a bug to me?

  • #2
    ApplicationListener not invoked on anonymous inner beans

    I have just found this to be true as well. Can someone from the Spring team tell me if this is expected behavior or a bug? It could be fixed in 2.0 if someone quickly responds I consider this a bug because there is nothing in the API or reference documentation which states you must be a top-level identified bean.
    Last edited by paul4christ79; Jul 12th, 2006, 11:53 PM.


    • #3
      Hi paul4christ79

      This is the expected behaviour. I will change the Javadoc and reference documentation to highlight this.

      To clarify the issue, anonymous beans are not simply beans that don't have a name. They are also totally scoped within the context of the surrounding bean (definition), and as such cannot be seen by other beans or indeed by other methods defined on the various Spring ApplicationContext interfaces (in this case 'getBeanOfType(ApplicationListener)').

      If you want to have the Spring ApplicationContext implementation that you are using automatically detect ApplicationListeners in your context, promote such listener beans to top level bean definitions.



      • #4
        Hi Rick,

        I just encountered this issue too. And it seems like a very odd choice of behaviour. We use templates extensively to define transaction properties. Like this.

            <bean id="systemManager" parent="txProxyTemplate">
                <property name="target">
                    <bean class=""/>
        But I can't easily use Spring event publishing infrastructure, by letting the class "" implement ApplicationListener.

        It seems a bit messy to have to have to make the internal bean to be a public top-level object just to that it can receive application events. This class should never be used not as a part of a transaction .... so shouldn't really be a public bean. It's exposing an implementation that I don't think should be exposed.

        Maybe I'm missing something here? Are there any alternatives?