Building the services using JBoss Microcontainer isn’t complicated at all; create necessary classes and wire them together using XML descriptor or couple of annotations. But unless you’re building just a very simple components, you might want to be able to hide the implementation and expose only the special interface (public) beans to the outside world. JBossMC has handy feature to support this using scoped kernel / controller.
Let’s start with the example:
<deployment xmlns="urn:jboss:bean-deployer:2.0">
<bean name="sample1" class="net.laststation.demo.mc.SampleBean">
<annotation>@org.jboss.metadata.plugins.scope.ApplicationScope("testApp")</annotation>
</bean>
<bean name="myService" class="net.laststation.demo.mc.MyServiceDemo">
<property name="holder"><search bean="sample1" type="leaves"/></property>
</bean>
</deployment>
In this deployment we’ve got a sample bean annotated by @ApplicationScope which will force sample1 to be instantiated within the application scope testApp. Publicly exposed service, represented by myService, requires this object, but instead of the regular injection it must apply the search method – using the XML element (as shown above) or annotation @Search, in both cases specifying the bean identification and required strategy.
Search strategies are implemented within the package org.jboss.dependency.plugins.graph and in general consists form the two kind of implementations, based on only the local scope or hierarchy. Current list includes:
- DEFAULT
- LOCAL
- TOP
- PARENT_ONLY
- PARENT
- DEPTH
- LEAVES
- WIDTH
- CHILD_ONLY_DEPTH
- CHILD_ONLY_LEAVES
- CHILD_ONLY_WIDTH
If you want to break the example, just change the search type to one that doesn’t take children scopes into account – for example TOP.
Below the line
Please note, the scoped kernel implementation is currently at the prototype stages. I wouldn’t expect significant changes in the functionality but configuration might be ‘adjusted’.
Personally, I would love to see support for the scope definition other than using annotations. Such an improvement would be much more coherent in the perspective of the meta data builder and XML configuration, where for example might be a place for the XML element <scope>.
Second comment goes to the requirement to use the search method instead of injection, for which the specification of the search strategy might be equally adequate.
One Trackback
[...] the whole application server. To prevent scanning of unnecessary code I had to use feature called scoped kernel/controller and force restful resources to be scoped into own context, where they can be easily [...]