At this years EclipseCon I implemented an extension factory utility that could be used to delegate the creation of an extension object to a spring context (if you are using
Spring Dynamic Modules). The result was that you were able to define your extensions as spring beans including all the nice spring features (dependency injection, aop, dessert topping, whatever...). That first implementation was nice, but had a number of stupid limitations (worked only for active bundles, for example).
The good news is: A new version is available that includes some nice improvements... :-)Firstofall, here is the new version of the Spring-Extension-Factory:
The new features are:
- Ability to declare the bean-id explicitly within the extension definition
- Automatic start of non-active bundles (and application context creation via Spring Dynamic Modules) when an extension is being created
How can I use this extension factory? Lets look at some examples. Lets assume you implement an RCP application and you would like to define a view as spring bean to inject some dependencies. First, you declare your spring bean inside the spring context:
<bean id="myview"
class="org.eclipse.example.springdm.rcpview.View"
scope="prototype">
<property name="myService" ref="serviceBean"/>
</bean>
Okay, this defines the spring bean called "myview" and injects a bean with the id "serviceBean" as a dependency into my view. The class of the view is the normal view implementation that you are familiar with. Don't forget to declare the bean as of scope "prototype". Otherwise spring would return always the same object every time an extension should be created (which is not the contract of the extension mechansim).
Now we define the extension in the plugin.xml file:
<extension point="org.eclipse.ui.views">
<view name="Message"
allowmultiple="true"
icon="icons/sample2.gif"
class="org.eclipse.springframework.
util.SpringExtensionFactory:myview"
id="org.eclipse.example.springdm.rcpview.view">
</view>
</extension>
The class that is now referenced in the extension instead of the view itself is the spring extension factory followed by a colon and the id of our bean from the spring context. The rest of the extension definition looks exactly as before (without the extension factory).
The Spring Extension Factory includes two additional options to refer to the bean id within the extension definition. This is the strategy that I implemented into the extension factory:
- If a bean-id is explicitly defined (like in the example above), use this id to lookup the bean.
- If not, search for an id attribute within the contribution element of the extension and use it as the bean-id.
- If no id attribute is found inside the contribution element, try to use the id of the extension itself as bean-id.
The first option is the most explicit one. The second option works just for those extension definitions where the corresponding extension point schema defines an id attribute as part of the specific extension (this is the case for views, but obviously not for all extension points). The third option can be used for all extension points but is less explicit that the first one. In the end its a matter of taste which option you prefer.
Comments, feedback and improvements, of course, highly welcome.
Have fun with it!!!
At this years EclipseCon I implemented an extension factory utility that could be used to delegate the creation of an extension object to a spring context (if you are using
Spring Dynamic Modules). The result was that you were able to define your extensions as spring beans including all the nice spring features (dependency injection, aop, dessert topping, whatever...). That first implementation was nice, but had a number of stupid limitations (worked only for active bundles, for example).
The good news is: A new version is available that includes some nice improvements... :-)Firstofall, here is the new version of the Spring-Extension-Factory:
The new features are:
- Ability to declare the bean-id explicitly within the extension definition
- Automatic start of non-active bundles (and application context creation via Spring Dynamic Modules) when an extension is being created
How can I use this extension factory? Lets look at some examples. Lets assume you implement an RCP application and you would like to define a view as spring bean to inject some dependencies. First, you declare your spring bean inside the spring context:
<bean id="myview"
class="org.eclipse.example.springdm.rcpview.View"
scope="prototype">
<property name="myService" ref="serviceBean"/>
</bean>
Okay, this defines the spring bean called "myview" and injects a bean with the id "serviceBean" as a dependency into my view. The class of the view is the normal view implementation that you are familiar with. Don't forget to declare the bean as of scope "prototype". Otherwise spring would return always the same object every time an extension should be created (which is not the contract of the extension mechansim).
Now we define the extension in the plugin.xml file:
<extension point="org.eclipse.ui.views">
<view name="Message"
allowmultiple="true"
icon="icons/sample2.gif"
class="org.eclipse.springframework.
util.SpringExtensionFactory:myview"
id="org.eclipse.example.springdm.rcpview.view">
</view>
</extension>
The class that is now referenced in the extension instead of the view itself is the spring extension factory followed by a colon and the id of our bean from the spring context. The rest of the extension definition looks exactly as before (without the extension factory).
The Spring Extension Factory includes two additional options to refer to the bean id within the extension definition. This is the strategy that I implemented into the extension factory:
- If a bean-id is explicitly defined (like in the example above), use this id to lookup the bean.
- If not, search for an id attribute within the contribution element of the extension and use it as the bean-id.
- If no id attribute is found inside the contribution element, try to use the id of the extension itself as bean-id.
The first option is the most explicit one. The second option works just for those extension definitions where the corresponding extension point schema defines an id attribute as part of the specific extension (this is the case for views, but obviously not for all extension points). The third option can be used for all extension points but is less explicit that the first one. In the end its a matter of taste which option you prefer.
Comments, feedback and improvements, of course, highly welcome.
Have fun with it!!!
Dependency Injection for Extensions, Second Edition