问题
There are lots of Eclipse RCP tutorials that begin with the obvious first step: "Create a new plugin project..."
It seems that approx. 70% of them specify checking the "Generate an activator, a Java class that controls the plug-in life cycle". The others specifically say don't check that toggle.
alt text http://img179.imageshack.us/img179/6710/newpluginoptions.png
Its not clear to me, what generating an activator class does for you, when you need one, and when you don't.
For being a prominent option you get every time you create a new plugin project (it seems to be set on by default) this option isn't very well explained anywhere that I have found.
Any advice/rules of thumb on choosing this option when creating Eclipse plugin projects?
回答1:
One way to find out is to look at the generated class. Turns out that it is a subclass of AbstractUIPlugin. Check out the JavaDoc, it provides services like preference management, image registry and the like. If you need any of this, you may want to use it. It is a subclass of Plugin, which kind of makes sense.
Also, it implements BundleActivator, which has some useful JavaDoc. This provides you with stubs for start() and stop(), which allows you to hook your own code in here. It also generates a static convenience method getDefault(), which gives you the Activator. And that's all there is to it.
回答2:
From Eclipse itself (context sensitive help for the dialog) it says this, which is marginally useful.
"An activator is a Java class that controls the plug-in's life cycle. It is only needed if you require to do work upon the startup or shutdown of your plug-in."
When turning this option ON, an Activator.java class is auto-generated for your new project.
So, it sounds like if (being somewhat a novice) you have no idea why or what additional work you have to do at plugin startup/shutdown, you can safely leave this OFF. Just one less .java file showing up in your project source folder.
回答3:
If you really want to know, take a look at the OSGi specification; release 4 is the current version. Since Eclipse 3, every plugin is an OSGi bundle. The bundle activator is notified when the bundle is started and stopped, which usually happens when Eclipse starts and shuts down. You can also install listeners that are notified when other bundles (i.e. plugins) are started or register OSGi services.
For example, I use a listener to start certain operations after my bundle has completed its startup; otherwise I may run into classloader issues. You may also need the activator to store the BundleContext, which lets you load classes and gives you access to the bundle's name and version.
回答4:
Here's the closest thing to an explanation I've found:
http://dev.eclipse.org/newslists/news.eclipse.platform.rcp/msg23445.html
来源:https://stackoverflow.com/questions/2857476/when-to-choose-generate-an-activator-when-creating-a-new-eclipse-plugin-pro