AnsweredAssumed Answered

Handle plugin lifecycle events in Plugin or Spring bean?

Question asked by Robert Hanson on Jul 17, 2014

When I use Maven to start a new plugin project I see this...

 

[from plugin.xml]

<class>com.example.plugin.TrainingPlugin</class>

 

[from spring.xml]

<beans xmlns="http://www.springframework.org/schema/beans"
       default-autowire="no" default-init-method="init" default-destroy-method="destroy">

    <bean class="com.example.plugin.TrainingPlugin"/>

...
</bean...s>

 

[from TrainingPlugin.java]

public class TrainingPlugin implements Plugin {
  public void initPlugin(){
  }

  public void destroy(){
  }
}

 

The tutorial (Creating Widgets)  says that you can handle lifecycle events by using <class> in the plugin.xml and then having the class implement the Plugin interface.

 

...But because the class is also registered as a Spring bean, with the default init method as init() and default destroy method as destroy(), it would accomplish the same thing.  Yes?

 

So, which is preferable to use?  Implement Plugin, or use a bean with an initializer?  Is there a difference?  Do they get called at different times?

 

I'm just trying to fully understand why you would use one over the other, or why you might use both.

Outcomes