Announcement Announcement Module
Collapse
No announcement yet.
HOW TO: Load JAWR configuration properties with Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Last edited by eliot; Nov 25th, 2008, 04:52 AM. Reason: JAWR 2.6+ now supports Spring out of the box

  • #2
    Thanks for the info Eliot!

    https://jawr.dev.java.net/ seems interesting.
    Last edited by thebugslayer; Aug 8th, 2008, 09:30 AM. Reason: correct mispelled name.

    Comment


    • #3
      Hi, I am the lead developer behind Jawr and I'd like to thank Eliot for keeping this post updated with valuable info for Jawr users.

      That said, as of version 2.6, Jawr supports Spring natively by providing a Controller implementation which can be configured using the standard Spring descriptor.

      More info at https: //jawr.dev.java.net/integration/spring.html
      [sorry about the non linked URL, but the forums won't allow me to post links until my 6th posting.]

      Regards,
      Jordi.

      Comment


      • #4
        Hi Jordi,

        Great to see native support for Spring in Jawr, thank you. The first post in this thread is prefixed with an update telling readers to check out https://jawr.dev.java.net/integration/spring.html

        Cheers,
        Eliot

        Comment


        • #5
          Hi Jordi,

          I am very impressed by JAWR you developed and found that it is very useful. I have a few questions:

          1. If an individual file (like js or css file) was changed, is JAWR triggered to rebuild the bundle without restart the server? I looked at the code and I have an impresssion that only the change of the jawr.properties will trigger the rebuilding feature.

          2. If I use JAWR spring controller instead, would any change to the jawr.properties or js /css file trigger bundle rebuilding without restarting appserver?

          thanks,
          -carol

          Comment


          • #6
            Hi Carol, thanks for the positive comments.

            Unfortunately the answers to your questions won't be too good... The spring plugin right now does not support reloading of the properties file, so you'd need to redeploy (or refresh the whole spring config, if that's possible) to be able to refresh the bundles.

            Of course, don't forget that the debug mode does of course allow for changing the script/css files and seeing changes right away. But I guess you meant refreshing in production, so sadly that's a no for now. I'll keep it in mind as a future fix, expect some changes in the next version.

            By the way, I recommend you to post future questions on the Jawr forum, since I check that one much more often and you are more likely to get a quick response.

            Regards,
            Jordi.

            Comment

            Working...
            X