Z2 Components and Spring Beans

Components in Z2 can be looked up using the IComponentsLookup interface or JNDI. Alternatively you can map Z2 components directly into your Spring application context using the Component Factory Bean (com.zfabrik.springframework.ComponentFactoryBean) that is exposed by the com.zfabrik.springframework project.

Given a component samples.spring.simplemodules.services/computations that implements the interface samples.services.IComputationService you can map it into your application context for the given type by adding the XML snippet

<bean id="computations" class="com.zfabrik.springframework.ComponentFactoryBean">
    <property name="componentName" value="samples.spring.simplemodules.services/computations"/>
    <property name="className" value="samples.services.IComputationService"/>
</bean>

Conversely you may want to make a bean available as a Z2 component, so that it can be looked up from other Z2 components or so that it may participate in system states life cycle for example. In that case you need to first make the application context known. That is necessary so that beans from it can be exposed while the application context is still a singleton. When looking up a component that is actually a Spring bean, its application context will be initialized first – if not already.

The following component declaration declares an application context to Z2:

com.zfabrik.component.type=org.springframework.context
#
# context config location is where the context is
# actually defined.
#
context.contextConfigLocation=classpath:META-INF/applicationContext.xml

Note: declaring a system state participation binds the application context to the system state life cycle and hence you can easily run application contexts independently from web apps. One prominent use case of this is to execute scheduled jobs on different worker processes than those serving a web frontend.

Having an application context bound as a Z2 component we can expose it's beans as well:

com.zfabrik.component.type=org.springframework.bean
#
# the context that defines the bean (more than one
# bean can be exposed like this)
#
bean.context=samples.spring.simplemodules.services/applicationContext
#
# the bean name
#
bean.name=computations