Jun 07 2009

Spring Framework XML Schema Extensions…

Category: JavaPhil @ 8:20 pm

Seems like every time I dig into Spring, I learn something new. This week, it was the Spring Util schema. Like most developers, I never take the time to read the manual and usually only have enough time to get the job done. Unfortunately, the rush to get done typically highlights some features that I really did not understand or used improperly. I seem to get a lot more out of reading the documentation after seeing how all of the pieces go together. Fortunately, I’m usually not satisfied with this level of mis-understanding, so you are forced to read about how I fixed my mistakes!
Generally, if you are only using the IoC container feature of Spring (dependency injection), you can usually get by using the simple bean schema. I have messed around with Aspect Oriented Programming (AOP) integration using the AOP schema, but never used it in a real project and I always use the Transaction annotations, so I never bother with the Transaction schema either! When I started integrating the Spring Security framework into my last project, my Spring XML files started looking a little different, I started qualifying all of my bean tags.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
                         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
                         http://www.springframework.org/schema/security
                         http://www.springframework.org/schema/security/spring-security-2.0.1.xsd">
<security:global-method-security
secured-annotations="enabled" jsr250-annotations="enabled">
</security:global-method-security>
<beans:beans id="dateTimeBean" class="org.beilers.DateTimeBean">
</beans:beans>
<beans:beans id="timeDependentBean" class="org.beilers.TimeDependentBean">
<beans:property name="currenttime">
<util:property-path id="name" path="dateTimeBean.currentTime" />
</beans:property>
</beans:beans>
</beans>

I will write about the Spring Security framework in future post, but I wanted to highlight the Spring Util schema. I ran into a scenario last week where I wanted to use a property in an existing bean, to set a value in another bean; I could not pass in the bean, only the property. With the standard bean tags, there is no way to navigate a bean property path, such as bean.bean2.property. I knew there had to be solution to this problem, it seemed too common. With a little Google-ing help from a friend, we ran across the Util schema. There are some really convenient tags to help you manage your Spring configuration files. In the above example, I demonstrated the property-path tag to demonstrate path navigation. There as also other tags for accessing constants and fields, creating reusable lists and maps. Make sure you give this document a read, it can save you time and make your code cleaner and even add some type-safety to your XML.

https://www.beilers.com/wp-content/plugins/sociofluid/images/digg_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/reddit_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/dzone_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/delicious_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/blinklist_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/blogmarks_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/google_48.png https://www.beilers.com/wp-content/plugins/sociofluid/images/facebook_48.png

Leave a Reply