oranjestad.spring.plugin
Class PluginBeanFactoryPostProcessor

java.lang.Object
  extended by oranjestad.spring.plugin.PluginBeanFactoryPostProcessor
All Implemented Interfaces:
org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.config.BeanFactoryPostProcessor

public class PluginBeanFactoryPostProcessor
extends java.lang.Object
implements org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.beans.factory.BeanNameAware

Utility used by spring to dynamically plug-in beans into extension beans. This approach was first publicized in the 2006 publication of devx magazine.

Typical spring allows you to directly wire together components. This post processor will insert a bean definition into a List based property of another bean just before object creation. This combined with springs auto discovery of context files allows you to wire together components without the original component's configuration getting modified.

Example

Imagine a bean context with the following contents. It defines a bean named extension.object with a property extProperty that is an empty list.

 <beans>
     <bean id="extension.object" class="some.class">
         <property name="extProperty">
             <list>
             <!-- properties typically added via plug-in mechanism -->
             </list>
         </property>
     </bean>
 </beans>
 
Now imagine we want to wire in a bean reference to extProperty but we don't want to modify this file. We could define a seperate context file as follows. It defines the plug-in bean and uses an instance of PluginBeanFactoryPostProcessor to wire in its reference.

 <beans>
     <bean class="oranjestad.spring.PluginBeanFactoryPostProcessor">
         <property name="extensionBeanName" value="extension.object" />
         <property name="propertyName" value="extProperty" />
         <property name="pluginBeanName" value="plugin" />
     </bean>

     <bean id="plugin" class="some.class.AppropriateForExtProperty" />
 </beans>
 

Usage

This class assumes the usage of spring and its configuration should look as follows:

     <bean class="platform.spring.PluginBeanFactoryPostProcessor">
         <property name="extensionBeanName" value="bean with list based property" />
         <property name="propertyName" value="list based property" />
         <property name="pluginBeanName" value="bean to plugin" />
     </bean>
 

Implementation Notes

In spring when using <import resource="classpath*:.."/> it is possible to pick up the same spring file multiple times (for example if a jar is on the classpath twice). Usually this is not a problem since spring uses a last-one-in-wins approach. But with plugins, it would be possible for a plug-in to get added twice, since this class might be created and registered twice.

To work around this case, this class only adds beans not currently in the extension points list of objects. If, however, the actual intent is to add the same bean twice, you can accomplish this by creating an alias to the same bean and adding that alias.

Since:
1.0
Author:
Bryant Harris

Constructor Summary
PluginBeanFactoryPostProcessor()
           
 
Method Summary
 void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
           
 void setBeanName(java.lang.String name)
           
 void setExtensionBeanName(java.lang.String beanName)
          The bean that is being extended (the bean with a List based property.
 void setPluginBeanName(java.lang.String pluginName)
          The name of the bean to plug-in to the extension bean's list property.
 void setPropertyName(java.lang.String propertyName)
          The name of the List property within the extension bean.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PluginBeanFactoryPostProcessor

public PluginBeanFactoryPostProcessor()
Method Detail

setExtensionBeanName

public void setExtensionBeanName(java.lang.String beanName)
The bean that is being extended (the bean with a List based property.

Parameters:
beanName - Spring bean name.
Since:
1.0

setPropertyName

public void setPropertyName(java.lang.String propertyName)
The name of the List property within the extension bean.

Parameters:
propertyName - property name.
Since:
1.0

setPluginBeanName

public void setPluginBeanName(java.lang.String pluginName)
The name of the bean to plug-in to the extension bean's list property.

Parameters:
pluginName - The plugin bean's name.
Since:
1.0

setBeanName

public void setBeanName(java.lang.String name)
Specified by:
setBeanName in interface org.springframework.beans.factory.BeanNameAware

postProcessBeanFactory

public void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
                            throws org.springframework.beans.BeansException
Specified by:
postProcessBeanFactory in interface org.springframework.beans.factory.config.BeanFactoryPostProcessor
Throws:
org.springframework.beans.BeansException


Copyright © 2008. All Rights Reserved.