Announcement Announcement Module
Collapse
No announcement yet.
Moving to spring web mvc: best practice loading *.properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Moving to spring web mvc: best practice loading *.properties

    Hi,

    Is there an established best practice in spring for loading .properties files on webapp startup time?

    I have perused the manual and looked at the step-by-step example online and searched this web forum but I have come up empty handed.

    Any tips for how to do this in Spring? I am converting a proprietary app which uses a servlet as part of a core library to load .properties files. (as defined in web.xml).

    Any help will be much appreciated.

    Jim

  • #2
    im not quite sure exactly what you are after, because Spring allows you to configure your beans with properties files without the need to write any code.

    If you needed to do your own loading in a Spring-managed bean i could suggest using Springs Resource abstraction framework.

    if you specify a property in a bean as a org.springframework.core.io.Resource Spring will try to reference that resource when it binds the property in the bean. for example, if you set the property to be
    "WEB-INF/resources/custom.properties" and you start Spring in a webapplication it will look for the resource from the root of your webappp. alternativately using "classpath:custom.properties" would look on the classpath

    you could then load it with
    Code:
     Properties p= new Properties();
    	    if (resource.exists()) {
    	        p.load(resource.getInputStream());
    	    }
    but if you give some more detail on exactly what u are trying to replace. might be able to help more

    Stuart.

    Comment


    • #3
      I have a Spring bean (a business object) that also needs to read in some files that are stored in the WAR. I simply do that in the init method of the bean, which will be called before the bean is put in use.

      S.

      Comment


      • #4
        Originally posted by stueccles
        If you specify a property in a bean as a org.springframework.core.io.Resource Spring will try to reference that resource when it binds the property in the bean.
        Oh that is nice. Didn't know about that one yet :-)

        There is so much in Spring that is barely documented. I can't wait for the book that the Spring gang is writing now. Hopefully it will touch many more subjects than 'J2EE Development without EJB' does.

        S.

        Comment


        • #5
          Spring allows you to configure your beans with properties fi

          Steccles,

          Thanks for the reply. I am new to spring and am coming at this from a struts background.

          Quite simply, my application needs to refer to the name value pairs in a number of .properties files and these should be stored in application scope.
          If it were struts, I would simply create a StartupServlet and read in the .properties files in the standard way and store them (in the StartupServlet's init method) in the application scope.

          You say, "Spring allows you to configure your beans with properties files without the need to write any code. " can you explain how tis is done (or point me to the place in the manual where this is explained)?

          Thanks
          Jim

          Comment


          • #6
            ok, gotcha

            Ah, sorry, I misread your first reply - yep I'll give that a try.

            Jim

            Comment


            • #7
              one thing ...

              Stueccles

              When you say, 'if you specify a property in a bean ...' do you mean defining an xml bean in applicationContent.xml? Or do you mean literally defining a class with a property and an intitial String value.

              If the latter is the case, then it means I'll have to choose a Resource implementation.

              Jim

              Comment


              • #8
                Hi Jim,

                I think you can achieve this by defining a class with a property which is then set in the application descriptor. You can then run the method on startup by using init-method.

                A little example from my SpringXMLDB code
                Code:
                	<bean id="uploadProcessor" class="com.lakeside.springxmldb.processor.XMLDBAutoLoader" depends-on="xmldatabase" init-method="init">
                		<property name="xmldbManageDao"><ref bean="xmlManageXMLDBDao"/></property>
                		<property name="collectionManageDao"><ref bean="collectionManageXMLDBDao"/></property>
                		<property name="initDirectory"><value>/WEB-INF/resources/content</value></property>
                		<property name="collection"><value>/db/tilesnews/items</value></property>
                	</bean>
                Code:
                public class XMLDBAutoLoader extends XMLDBUploaderProcessor &#123;
                
                	protected Resource initDirectory;
                	
                	protected String collection ="/db";
                	
                	/**
                	 * @param initDirectory The initDirectory to set.
                	 */
                	public void setInitDirectory&#40;Resource initDirectory&#41; &#123;
                		this.initDirectory = initDirectory;
                	&#125;
                	
                	/**
                	 * @param collection The collection to set.
                	 */
                	public void setCollection&#40;String collection&#41; &#123;
                		this.collection = collection;
                	&#125;
                	
                	/**
                	 * run the upload directory and recursively upload the directory resource
                	 * specified to the xml database
                	 * @throws IOException if the resource cannot be found
                	 */
                	public void init&#40;&#41; throws IOException &#123;
                		if &#40;initDirectory.exists&#40;&#41;&#41; &#123;
                			//load the initial directory
                			this.uploadDirectory&#40;initDirectory.getFile&#40;&#41;, collection, true&#41;;
                		&#125;
                	    else &#123;
                	    	throw new IOException&#40;"resource can not be found at " + initDirectory&#41;;
                	    &#125;
                	&#125;
                	
                &#125;
                When the application context is started up this will cause init-method to run and load the file specified in the resource which will be loaded from the WAR.

                The init-method is used to run methods after properties have been set and the context is loaded.

                Alternatievely u could move all the properties from your properties files to
                the XML applicationContext.xml and specify in XML with
                Code:
                <property name="mappings">
                			<props>
                				<prop key="key1">propvalue</prop>
                				<prop key="key2">propvalue</prop>
                				<prop key="key3">propvalue</prop>
                			</props>
                </property>

                Comment


                • #9
                  loading properties file on webapp startup

                  Thanks Stuart, its working now.

                  I just had to get my applicationContext file loading first.

                  Cheers mate

                  Jim :wink:

                  Comment


                  • #10
                    Thanks lot for sharing the property code.

                    Choose the Best of London Properties

                    Comment

                    Working...
                    X