Monday, October 11, 2010

Creating your own custom Spring configuration.

Recently I volunteered myself to do some of the Spring integration for Hazelcast, for one main reason actually.. I had always wondered how to actually do that. It is something you don't get to do often in a corporate working environment. As with most things Spring related it actually turned out to be very simple.

First thing I needed to do was define a schema for the Hazelcast xml config. If you want to add your custom XML configuration into Springs', it will require that you have a schema for the specific XML.

So what is the quickest way to get started on a schema? Altova XML Spy, it generates a basic XSD form a sample XML which takes care of about 40% of the actual work. Then it's all about the details, min / max constraints, enumerations, defaults, attributes and documentation. I believe XSDs should be verbose, so the version below is not 100% complete but it is a good starting point.
I am not going to add the XSD to this post as is quite long and doesn't add much with regards to the actual Spring integration. If interested, it is available here...

On the Spring side you need the following:
1. An implementation of: NamespaceHandlerSupport, this basically tells Spring when it finds "hazelcast" use the "HazelcastBeanDefinitionParser".



2. An implementation of: AbstractSimpleBeanDefinitionParser. This implementation actually processes the XML from the Spring application config. You need to override
getBeanClass and doParse methods, to specify what type of object and how to create it respectively.


3. 2 files in the META-INF
3.1. A spring.handlers file containing the namespace and the NamespaceHandlerSupport implementation (in my case):
http\://www.hazelcast.com/schema/config=com.hazelcast.spring.HazelcastNamespaceHandler

3.2. A spring.schemas file containing the location of the schema:
http\://www.hazelcast.com/schema/config/hazelcast-basic.xsd=hazelcast-basic.xsd

and that is it...

The XML you described in your XSD is now available to be used within your the Spring config.

The example below is a Hazelcast map cache config, which can then be injected where required as ref="config".



The source and tests for this are available in the Hazelcast code project here.

7 comments:

  1. Is the schema location correct?

    http://www.hazelcast.com/schema/config/hazelcast-basic.xsd

    ReplyDelete
  2. Hi,

    Nope, they never did publish it to that URL. This was just an example and the initial version I did for them and unfortunately I haven't had any time to dedicate much more to Hazelcast.

    ReplyDelete
  3. This is cool. Thanks, Brian. Even though I am reading it much later than when you published it, the information is very valuable.

    ReplyDelete
  4. Hello sir, I got you by Google search.

    It is really helpful article about the topic of briandupreez.net.It is very helpful in using social media sites. I so much interest about your article. I know it is not any simple information. This is very nice post! I will bookmark this blog as I have Greater Eviction Service Solutions provides fast, efficient, and professional eviction services for property owners in Austin, Florida. Demand an eviction these days.

    Thanks for the great post.

    ReplyDelete
  5. It's nice that there are such blogs.

    ReplyDelete

Popular Posts

Followers