HOW TO: Load JAWR configuration properties with Spring
**********************************
UPDATE: Jawr 2.6+ now supports Spring without the code below, for the howto see https://jawr.dev.java.net/integration/spring.html
If you're using Jawr version 2.4.* or 2.5.* then you can either upgrade to 2.6+ to get Spring support or use the code below.
**********************************
Hi all,
Very surprised to find no chatter about jawr in these forums. I'm not affiliated with the project at all but am a very recent impressed user. Anyway, for those who know about jawr here's a quick guide on how to load jawr.properties using Spring:
Create a class like this:
Code:
package foo.bar;
import java.util.Properties;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import net.jawr.web.resource.bundle.factory.util.PropsFilePropertiesSource;
import net.jawr.web.resource.bundle.factory.util.ServletContextAware;
/**
* Properties source to use with jawr in web.xml takes its properties from the properties bean specified in the
* spring context with name "jawrProperties". Note the ServletContextAware interface is from jawr, not spring.
*
*/
public class JawrPropertiesSource extends PropsFilePropertiesSource implements ServletContextAware {
private ServletContext servletContext;
/**
* This method implemented from ServletContextAware so we can get a reference to the ServletContext
* to get access to the spring app context.
*/
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
// For JAWR 2.5+ name method doReadConfig(), for earlier versions, name method getConfigProperties()
protected Properties doReadConfig() {
// public Properties getConfigProperties() { // Rename the method to this if you are using JAWR 2.4.2 or less
// Get the spring app context.
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
// Get the jawrProperties from the spring app context.
return (Properties)ctx.getBean("jawrProperties");
}
}
Then in web.xml, specify a custom configPropertiesSourceClass using init-param:
Code:
<servlet>
<servlet-name>JavascriptServlet</servlet-name>
<servlet-class>net.jawr.web.servlet.JawrServlet</servlet-class>
<init-param>
<param-name>configPropertiesSourceClass</param-name>
<param-value>foo.bar.JawrPropertiesSource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
And finally in your spring config, you'll need a bean like this:
Code:
<bean id="jawrProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:jawr.properties"/>
<!-- Optional, properties specified here will override any properties specified in jawr.properties -->
<property name="localOverride" value="true"/>
<property name="properties">
<props>
<prop key="jawr.debug.on">${jawr.debug.on}</prop>
</props>
</property>
</bean>
And that's it.
JAWR is used to compress and serve JavaScript and CSS resources. it solves all these issues:
- Browser caching old js and css files
- Minifies JavaScript
- Correct any relative paths in css files
- Combines JavaScript or CSS files into a single file (or multiple files if you want)
Best regards,
Eliot