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

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


    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.


  • #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 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/" and you start Spring in a webapplication it will look for the resource from the root of your webappp. alternativately using "" would look on the classpath

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



    • #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.



      • #4
        Originally posted by stueccles
        If you specify a property in a bean as a 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.



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


          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)?



          • #6
            ok, gotcha

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



            • #7
              one thing ...


              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.



              • #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
                	<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>
                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;
                	 * @param collection The collection to set.
                	public void setCollection&#40;String collection&#41; &#123;
                		this.collection = collection;
                	 * 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;;
                	    else &#123;
                	    	throw new IOException&#40;"resource can not be found at " + initDirectory&#41;;
                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
                <property name="mappings">
                				<prop key="key1">propvalue</prop>
                				<prop key="key2">propvalue</prop>
                				<prop key="key3">propvalue</prop>


                • #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:


                  • #10
                    Thanks lot for sharing the property code.

                    Choose the Best of London Properties