symfony2事件调度程序延迟加载监听器

I'm creating some of my own events using Symfony Event Dispatcher, which works fine.

I noticed however, that the listener I configured in symfony is not lazy loaded, it is always initialized. It is rarely used however.

The config in my services.yml looks like:

my.handler:
    class: Acme\AcmeBundle\DependencyInjection\MyHandler
    arguments:
      - @my.dependency
    tags:
      - { name: kernel.event_listener, event: my.event, method: handle }

Is there a way to configure this in such a way that @my.handler is only initialised when the event is fired? Because now it is initialised (along with all its dependencies) when it is pushed in the Dispatcher.

There is documentation about a ContainerAwareEventDispatcher: http://symfony.com/doc/current/components/event_dispatcher/container_aware_dispatcher.html But this only explains how to use it directly in PHP, not how to configure it in a standard symfony2 project.

You can define it as a Lazy Services adding the relative tag (as described here ) as example:

my.handler:
    class: Acme\AcmeBundle\DependencyInjection\MyHandler
    lazy: true
    arguments:
      - @my.dependency
    tags:
      - { name: kernel.event_listener, event: my.event, method: handle }

Remember to install the ProxyManager bridge as described in the doc.

Hope this help