Announcement Announcement Module
Collapse
No announcement yet.
Access external properties file from SPRING context file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access external properties file from SPRING context file

    Hi I have an Spring/BlazeDS application running on a WebSphere server. In my applicationcontext.xml file, I point to applicationcontext.properties file which contain environment specific infomormation such as database connection and urls, username and password and other webservices url. These information is different for different environment such as staging and production. Now I want to keep my applicationcontext.properties file in a separate location from my EAR file. How can i read this file from the applicationcontext.xml file so that I dont need to modify the path manually when deploying the app in different environment.

    Code:
    <bean id="propertyConfigurer"
    	   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	   <property name="location"><value>file:///c:/test/application.properties</value></property>
    	</bean>
    Any help will be highly appreciated. Thanks.

  • #2
    Try using import like this: <import resource="x.properties" /> or <import resource="classpath:x.properties" />

    Comment


    • #3
      Thanks for the reply venkrishy.

      I tried to use import instead of PropertyPlaceholderConfigurer, it fails to initialize the application. Could you please provide me with snippet?

      here is what I did to my applicationContext.xml file.

      Code:
      <beans>
      	<import resource="file:///c:/test/application.properties" />
      Thanks again.

      Comment


      • #4
        Looks like you need to specify the context namespace. See the reference for details on using it. What is the error you are getting?

        If you are having problems specifying the resource location, you might look at the Spring Springs Resource abstraction in the reference. Two quick options are
        1. specify a location on disk and guarantee that is where the properties file is found.
        2. Use classpath feature and place it where the classloader will pick it up. Options for WAS include placing it in the lib/ext, using a shared library, adding an new entry to the classpath of the servlet container. For details on this consult IBM's documentation.

        PS: This is probably a better question for the Core forums.

        Comment


        • #5
          Here is a complete example using spring 3. Look in spring example files etc, there should be more in there. I haven't used the file:// protocol, I am not sure why you have to do that. I place the property file either in the classpath or keep it relative to where I am calling it.

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

          <!-- Scans the classpath of this application for @Components to deploy as beans -->
          <context:component-scan base-package="com" />
          <import resource="mvc-config.xml" />

          </beans>

          Comment


          • #6
            Thanks Venkrishy and rwinch for the response. I am not importing an XML file. I am trying to read a properties file as in
            http://static.springsource.org/sprin...lderconfigurer

            The reason I need to do this is the properties of the file changes depending on the environment I am deploying my application on. I want to sort of automate it so that I do not need to manually change the properties and deploy my app. for instance, my database connection properties changes on dev environment and is different on stage and production environment. If I include my properties file within in my EAR file I will have to manually change those properties and create the EAR file. This is what I am trying to avoid. I want to keep that properties file somewhere out of the EAR file and then refer to it from Spring context. I used "file" option and works. But then again, what if my file location in those environment is different then it wont work. So this is where I need to either create a WebSphere url resource or class variable to access this file. I am trying to do it using the classpath mechanism as suggested by "rwinch" I am not been able to. If anyone of you can provide me a way to do it would be a great help.

            Thanks.

            Comment


            • #7
              That's a common scenario. Have all the various property files inside the war/ear file under an environment folder. So the directory structure would be DEV/filename.properties, STAGING/filename.properties, PROD/filename.properties etc. We then load the specific file using this syntax:
              <import resource="#{systemProperties['environment']}/startup.properties" />

              This will work as long as there is a system variable called 'environment' in each of those boxes set correctly. For example, the development box should have this system variable with the value equal to DEV, the staging box will have a value equal to STAGING etc. You can either create an environment variable like is normally done in Unix & Windows, or you can pass it in as a JVM parameter when starting up the server (like java -Denvironment=PROD).

              Play around with the #{systemProperties['environment_variable_name']} syntax and then you will understand how to solve this.

              Comment


              • #8
                Thanks to both of you. I used the WebSphere JVM argument to specify the properties files. I read the properties files from the applicationContex.xml. I got some help from this post as well.

                http://stackoverflow.com/questions/2...tem-properties

                Thanks again for all your help.

                Comment


                • #9
                  I tried many time and found below setting works. You can change value= "file:C:/.." for Windows.

                  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer"*
                  <property name="location" value="file:/etc/ve/config.properties" />
                  </bean>

                  Comment


                  • #10
                    Access external properties file from SPRING context file

                    would you please reply with full code to see how the properties are passed to the datasource cause I have used the configurator and have pojo file that with the properties to connect to the database but still I cannot get connect.

                    Comment


                    • #11
                      Originally posted by nepalibabu View Post
                      Thanks to both of you. I used the WebSphere JVM argument to specify the properties files. I read the properties files from the applicationContex.xml. I got some help from this post as well.

                      http://stackoverflow.com/questions/2...tem-properties

                      Thanks again for all your help.
                      Hi, I am trying to understand how to use jvm arguments to pick up correct resource files at runtime. Also How can I read values from that resource file in my spring.xml file. Can u suggest any good hands on example..thanks

                      Comment


                      • #12
                        context namespace is also import..

                        Comment

                        Working...
                        X