Thursday, March 11, 2010

How to avoid Spring validation errors: Unable to locate Spring Namespace handler , Incorrect usage of element for certain elements , No setter found for property , Class Not Found

I integrated spring IDE plug-ins in my eclipse environment. I wanted use some additional namespaces like camel, CXF and some custom schema to spring.xml, spring beans file. When I tried to validate the spring.xml file the spring IDE it was showing following errors and warnings in "Problems" / "Error Log" view of eclipse

  •  Unable to locate Spring Namespace handler...
  •  Incorrect usage of element for certain elements ... (The element was refereed form my custom schema)
  •  Many errors of Class Not Found.
  •  No setter found for property ... (When I am trying to set some property in spring.xml) 

Here, I am going to discuss the approaches I used to solve these issues.

Issue 1: Unable to locate Spring Namespace handler...
1) To resolve this issue there must be Namespace Handler implemented for the namespaces used in the spring beans (spring.xml) file. Now add the jar file containing NamespaceHandler implementation and all other dependent jars to the spring project's class path. This would probably solve this issue, but still this error is not resolved for me. In this case I have applied second solution of contributing NamespaceHandler using eclipse plug-in as follows:.
- You can create new eclipse plug-in or use any existing eclipse plug-in for this purpose
- First copy jar file containing NamespaceHandler class and all other dependent jar files to the plug-in. (e.g. I copied them to "lib" directory of my plug-in)
- Now open plugin.xml using plug-in editor in the eclipse and go to "Runtime" tab. Here in the "Classpath" section add all the jars which we have copied. (Before this make sure that .classpath file is not read-only)
- This will add all the jars in to project as well as plug-in's run-time classpath.
- Then configure plug-in dependencies by going to "Dependencies" tab. Here add the dependencies to following plug-ins if they are not already added.
  • org.springframework.ide.eclipse.beans.core
  • org.eclipse.ui
  • org.eclipse.core.runtime

- Note: you can also add the more plugin dependencies if they are required for the NamespaceHandler to work.
- After this you can contribute to spring NamespaceHandler using extension; you can switch to "Extensions" tab for this.
- Here, add the extension "org.springframework.ide.eclipse.beans.core.namespaces" by clicking on the add button. This will create extension entry as shown in the following image.
- If you look at source of plugin.xml file it would look like:

- As shown in above screen-shot we need to provide actual namespace URI as value of "uri", the fully qualified class name of NamespaceHandler class as value if "namespaceHandler". You can give some meaningful "name" (optional) to this contribution. If you have your custom element provider then provide its fully qualified name in "elementProvider" (optional).
- If you have more than one custom namespaces refereed in spring.xml then you can either add multiple extension-point contribution as shown above or you can just add multiple namespace entries withing same extension-point contribution as follows:
  • Right click on extension-point contribution to open the context menu; click on new -> namespace. This will add the new NamespaceHandler contribution entry and configure required required values as I have mentioned above.

- Now launch the eclipse with this plug-in contribution and it would resolve all warnings related to Unable to locate Spring Namespace handler...

 Issue 2 : Incorrect usage of element for certain elements ... (The element was refereed form my custom schema)
If you have contributed NamespaceHandler as shown in the above solution and your namespace handler properly handles the validation for the different element of the custom schema, then this issue will also be resolved as part of it.

Issue 3: Many errors of Class Not Found.
This error can come in the "Problems" view or in the "Error Log" view you might find stack trace stating ClassNotFoundExceotion or NoClassDefError. There are 2 scenarios when this error could come
  1. NamespaceHandler class is directly or indirectly referring that particular class, and it is not part of plug-in (The plug-in using which we contributed NamespaceHandlers) classpath.
    • This you can determine by looking at the stack trace of that error. If stack trace some where refers to Namespace handler or its relevant classes then the particular class is referred directly or indirectly from Namespace handler.
    • In this case pick up the jar file containing missing class in it and copy it to plug-in (e.g. "lib" directory of plug-in) and then add it to runtime classpath of the plug-in as I have mentioned in the Issue-1 and this would get rid of particular class not found error
  2. Bean defined inside spring.xml  file is referring, that class is missing or it is internally using this class.
    • Here you check the error in "Problems" view and check if this class is used to create the bean in side spring,xml or not? or else this class is referred in directly from one of the beans defined in the spring.xml
    • To solve this issue we need to determine the required jars for the all missing classes and then add all the required jar to Spring project's class path. (Here it is not required to copy jar files to project it self you can refer them as external jars also)
Issue 4: No setter found for property ... (When I am trying to set some property in spring.xml)
This error comes in "Problems" view of the eclipse. It could have occurred because of 2 reasons
  1. The property which we are trying to set on the bean, for the same setter method was not defined in the class.
    • In this case define setter method withing class definition as "setPropertyName(...)" with required logic.
  2. The setter method for that particular property does not belongs to same class but it belongs to parent or super parent class, but still you are getting "No setter found for property ..." error for the same property
    • In this scenario, you need to verify that all the classes in the hierarchy and all the class which it may directly or indirectly refer to must be present in "Spring" project's class path. (if they are present in third-party jars then those jars should be part of "Spring" project's classpath).

Share |

No comments yet


Embed Wave to Blogger