<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>2Paths &#187; spring</title>
	<atom:link href="http://www.2paths.com/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.2paths.com</link>
	<description>Custom Software Technical Architecture, Design and Development in Vancouver, BC, Canada</description>
	<lastBuildDate>Mon, 27 Sep 2010 01:15:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Locale aware caching</title>
		<link>http://www.2paths.com/2009/01/29/locale-aware-caching/</link>
		<comments>http://www.2paths.com/2009/01/29/locale-aware-caching/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 01:09:10 +0000</pubDate>
		<dc:creator>Geoff</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Under the hood]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[locale]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.2paths.com/?p=492</guid>
		<description><![CDATA[To help boost the performance of our webapps we like to implement caching. One nice feature of the Spring Framework is its support for AOP features within applications. We utilise this to implement AOP method caching within our Spring enabled web applications. This allows us to write code without needing to worry too much about [...]]]></description>
			<content:encoded><![CDATA[<p>To help boost the performance of our webapps we like to implement caching. One nice feature of the Spring Framework is its support for <a href="http://www.springframework.org/docs/reference/aop.html">AOP features</a> within applications. We utilise this to implement AOP method caching within our Spring enabled web applications. This allows us to write code without needing to worry too much about direct caching as we can easily enable it at the method invocation level with some simple Spring config chicanery.</p>
<p>The <a href="https://springmodules.dev.java.net/docs/reference/0.8/html/cache.html">SpringModules cache</a> extension provides the legwork for implementing this and Spring AOP the glue. One limitation of this implementation out of the box is that it is not Locale aware. Spring nicely provides a <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/context/i18n/LocaleContextHolder.html">LocaleContextHolder</a> to enable disparate components within the app stack to be locale aware so we needed to leverage this in order to ensure our method caching was able to not only cache method invocations with the same argument signature, but also within the same Locale (not something passed as arguments) .</p>
<p>Below is the meat of our simple solution, a LocaleAwareCacheKeyGenerator:</p>
<pre class="brush: java">
    public final Serializable generateKey(MethodInvocation methodInvocation) {

        Method method = methodInvocation.getMethod();

        HashCodeCalculator hashCodeCalculator = new HashCodeCalculator();
        hashCodeCalculator.append(method.hashCode());

        // now suck in the locale
        Locale locale = LocaleContextHolder.getLocale();

        // and use the bit we care about for hashcode generation
        if (considerLanguageOnly) {
            hashCodeCalculator.append(locale.getLanguage().hashCode());
        } else {
            hashCodeCalculator.append(locale.hashCode());
        }

        // now try and sort out things for the method args
        Object[] methodArguments = methodInvocation.getArguments();

        if (methodArguments != null) {
            int methodArgumentCount = methodArguments.length;

            for (int i = 0; i &lt; methodArgumentCount; i++) {
                Object methodArgument = methodArguments[i];
                int hash = 0;

                if (generateArgumentHashCode) {
                    hash = Reflections.reflectionHashCode(methodArgument);
                } else {
                    hash = Objects.nullSafeHashCode(methodArgument);
                }

                hashCodeCalculator.append(hash);
            }
        }

        return new HashCodeCacheKey(hashCodeCalculator.getCheckSum(), hashCodeCalculator.getHashCode());
    }
</pre>
<p>This class simply adds the Locale to the code used in the HashCodeChacheKeyGenerator supplied with Spring. So now we can easily enable locale aware AOP configured method invocation caching transparently to our application code.</p>
<p>Nice!</p>
<p>P.S. An eclipse project with code/test samples is <a href="http://www.2paths.com/static/blog/LocaleCacheKey.zip">available</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.2paths.com/2009/01/29/locale-aware-caching/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Spring themes to override resource bundle messages</title>
		<link>http://www.2paths.com/2008/10/17/spring-themes-and-resource-bundles/</link>
		<comments>http://www.2paths.com/2008/10/17/spring-themes-and-resource-bundles/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 19:00:33 +0000</pubDate>
		<dc:creator>Geoff</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Under the hood]]></category>
		<category><![CDATA[bundle]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[theme]]></category>

		<guid isPermaLink="false">http://blog.2paths.com/?p=94</guid>
		<description><![CDATA[We use Spring quite a bit. In one project we use Spring MVC (I won&#8217;t get into a rant about whether that&#8217;s a good thing or not) and rightly or wrongly we use Spring themes within our UI. Yes I know JSP based UIs are so last century but sometimes if the shoe fits &#8230;
One [...]]]></description>
			<content:encoded><![CDATA[<p>We use Spring quite a bit. In one project we use Spring MVC (I won&#8217;t get into a rant about whether that&#8217;s a good thing or not) and rightly or wrongly we use Spring themes within our UI. Yes I know JSP based UIs are so last century but sometimes if the shoe fits &#8230;</p>
<p>One thing that we wanted to do was have themes which override certain resource bundle variables defined in an application wide, internationalized properties file. For example, our login greeting is &#8220;Welcome&#8221; with an appropriate i18n version for various locales. We also have certain themes based on types of user accounts. Some themes require their own customized login greeting and others will rely on using messages from the default MessageSource.</p>
<p>Our Spring application utilizes two classes for accessing resource bundles:</p>
<ul>
<li><code>org.springframework.context.support.ResourceBundleMessageSource</code></li>
<li><code>org.springframework.ui.context.support.ResourceBundleThemeSource</code></li>
</ul>
<p>Both these classes provide access to specific resource bundles but do not necessarily know about each other which means the operate in isolation. Ideally we wanted to have a hierarchy of resource bundles such that a resource message defined in the bundle accessed by <code>ResourceBundleMessageSource</code> could be overridden by a message defined in resource bundles accessed by <code>ResourceBundleThemeSource</code>. Whilst the <code>ResourceBundleMessageSource</code> class has a <code>setParentResourceBundle()</code>, there is no way to inject that to the <code>ResourceBundleThemeSource</code> via configuration.</p>
<p>As the resourceful (no pun intended) java developers that we are, we decided to create our own wrapper class for theme bundle resolution. This class will use the application&#8217;s <code>ResourceBundleMessageSource</code> definition as a parent for its own resource message source so that we can ensure that if a message cannot be resolved by key in the theme bundle, it will then defer to the parent resource bundle (which is the application level MessageSource) and try and look up the key there. This simple class is outlined below:</p>
<pre class="brush: java">
public class CustomResourceBundleThemeSource
        extends ResourceBundleThemeSource {

    private MessageSource parent;

    public void setParentMessageSource(MessageSource messageSource) {
        parent = messageSource;
    }

    @Override
    protected MessageSource createMessageSource(String basename) {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename(basename);
        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setParentMessageSource(parent);
        return messageSource;
    }
}
</pre>
<p>With this class, we just need the following in our Spring config class:</p>
<pre class="brush: xml">

    &lt;bean id=&quot;messageSource&quot;
          class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot;&gt;
        &lt;property name=&quot;basenames&quot;&gt;
            &lt;list&gt;
                &lt;value&gt;bundles/messages&lt;/value&gt;
            &lt;/list&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    &lt;bean id=&quot;themeSource&quot;
          class=&quot;CustomResourceBundleThemeSource&quot;&gt;
        &lt;property name=&quot;basenamePrefix&quot; value=&quot;/themes/&quot; /&gt;
        &lt;property name=&quot;parentMessageSource&quot; ref=&quot;messageSource&quot; /&gt;
    &lt;/bean&gt;
</pre>
<p>Now whenever we need to resolve resource messages, we use the Spring specific theme tags:</p>
<pre class="brush: xml">
    &lt;spring:theme code=&quot;login.greeting&quot; /&gt;
</pre>
<p>And if we have a definition in our theme resource bundle we see that else we see the default definition as specified in the underlying MessageSource.</p>
<p>This allows us to override resource bundle messages for various themes as required and removed quite a bit of ugly JSP/JSTL logic for resolving messages conditionally.</p>
<p>Job done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.2paths.com/2008/10/17/spring-themes-and-resource-bundles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Webapp Configuration &#8211; the Spring Way</title>
		<link>http://www.2paths.com/2007/11/08/webapp-configuration-the-spring-way/</link>
		<comments>http://www.2paths.com/2007/11/08/webapp-configuration-the-spring-way/#comments</comments>
		<pubDate>Fri, 09 Nov 2007 01:37:09 +0000</pubDate>
		<dc:creator>Geoff</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Under the hood]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.2paths.com/webapp-configuration-the-spring-way.html</guid>
		<description><![CDATA[Most Java applications will need some context specific configuration when deploying to a certain environment. This configuration could be username, password and JDBC URL for a specific database configuration or the details of an SMTP server for java mail deployments amongst others.
There are many ways to to provide runtime configurations for Java applications. For web [...]]]></description>
			<content:encoded><![CDATA[<p>Most Java applications will need some context specific configuration when deploying to a certain environment. This configuration could be username, password and JDBC URL for a specific database configuration or the details of an SMTP server for java mail deployments amongst others.</p>
<p>There are many ways to to provide runtime configurations for Java applications. For web applications, two popular mechanisms are the use JNDI configurations managers and using properties files. Both mechanisms have their limitations and caveats.</p>
<p>When using The Spring Framework, we are presented with a few more options to enable configuring web app projects.</p>
<p>The traditional approach involves using property placeholders which are then munged at runtime with a specific properties file to ensure the placeholders have valid values before executing the application. Another approach is using the newer (Spring 2.0) PropertOverrideConfigurer to override pbean definition properties at application startup. This approach allows the default spring config file to be usuable w/out needing a valid property file as no placeholders exist in the config itself but properties are merely overridden at runtime if need be. This is slightly nicer as it means that a default spring context XML config file will run w/out needing any special property merging. This allows for projects to be run within an IDE (like Eclipse) without having to jump through hoops or run special ant/maven targets to stick the correct properties file in the classpath when wanting to run your app embedded in Eclipse.</p>
<p>Essentially, the default Spring context configuration will work out of the box but can be overridden at runtime with other property file based properties to ensure things run properly at deployment. It&#8217;s not that dissimilar to the PropertPlaceholderConfigurer but allows for an easier workflow when wishing to work seamlessly with an IDE.</p>
<p>Not sure if any of this makes sense but there is more info out on the InterWeb:</p>
<ul>
<li><a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/beans/factory/config/PropertyOverrideConfigurer.html">PropertyOverrideConfigurer</a></li>
<li><a href="http://blog.arendsen.net/index.php/2005/03/12/configuration-management-with-spring/">configuration-management-with-spring</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.2paths.com/2007/11/08/webapp-configuration-the-spring-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

