Announcement Announcement Module
Collapse
No announcement yet.
How to use property files Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use property files

    How can I use a property file in Spring? I have been trying it for a couple of hours, but my property variables are never replaces with their values:

    Code:
    <bean 	id="mail2mailPathConfig"
    			class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		
    		<property name="location">
    			<value>WEB-INF/mail2mailpath.properties</value>
    		</property>
    	</bean>
    	
    	<bean 	id="mail2mailPaden" 
    			class="mail2mail.constanten.MailtomailConfigdata"
    			depends-on="mail2mailPathConfig">
    			
        	<property name="sjabloonDir">
        		<value>$&#123;mail2mail.dir.sjabloon&#125;</value>
        	</property>
        	
        	<property name="mailXslPath">
        		<value>$&#123;mail2mail.path.mail.xsl&#125;</value>
        	</property>
        	
        	<property name="onderhoudXslPath">
        		<value>$&#123;mail2mail.path.onderhoud.xsl&#125;</value>
        	</property>
        	
        	<property name="afbeeldingDir">
        		<value>$&#123;mail2mail.dir.afbeelding&#125;</value>
        	</property>
        	
        	<property name="httpImagesDir">
        		<value>$&#123;mail2mail.dir.httpImages&#125;</value>
        	</property>    	    
    	</bean>
    And the properties.
    Code:
    #directory aangeven met dir
    #file aangeven met file
    mail2mail.dir.sjabloon = \\\\philip\\data\\Projecten\\Mail to Mail\\Sjablonen\\
    mail2mail.path.mail.xsl = \\\\philip\\data\\Projecten\\Mail to Mail\\Sjablonen\\xsl\\mail.xsl
    mail2mail.path.onderhoud.xsl = \\\\philip\\data\\Projecten\\Mail to Mail\\Sjablonen\\xsl\\onderhoud.xsl
    mail2mail.dir.afbeelding = \\\\philip\\data\\Projecten\\Mail to Mail\\Afbeeldingen\\
    mail2mail.dir.httpImages = http&#58;//philip/mailtomailimages/
    What am I doing wrong??

  • #2
    the depends-on attribute is useless.
    How do you run this piece of configuration ? It should be in an application context (what is used for servlet configuration with spring web mvc) so that the placeholder configurer is automatically enabled (it is manual in the case of a simple bean factory).
    Is there anything relevant to configuration in the logs produced by running your code ?

    Comment


    • #3
      I have been trying a lot of things, that is why the depends-on was there ( I saw it somewhere)

      I start the Spring context with:

      Code:
      <listener>		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      in my web.xml. (It is used in a webapplication).

      Everything else works great, even the JMX-ified beans.

      And I don`t get any feedback why the properties are not replaced.

      [edit]
      If I enter a bad path for the property file, spring complains:

      Code:
      <bean 	id="mail2mailPathConfig"
      			class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      
      		<property name="locations">
         			<list>
            			<value>WEB-INF/foobar.properties</value>
         			</list>
      		</property>
      
      	</bean>
      So Spring is using my property file... (if I enter the correct name), but nothing apears to be done with it.

      Comment


      • #4
        If I enter a non existing name for a property in the applicationConstant eg ${sillyname}, spring complains. So Spring knows the variables I have declared in the property file.. but it just doesn`t replace them.

        Comment


        • #5
          Try with a leading slash in the path:
          Code:
          	<bean id="mail2mailPathConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          		<property name="location"><value>/WEB-INF/mail2mailpath.properties</value></property>
          	</bean>

          Comment


          • #6
            I have tried it but no result.

            I have tried 'easier' properties a,b,c,d,e and no result.

            Spring just doesn`t replace ${propertyname} with the property value. THe path components gets initialized with all the ${propertyname} strings. But if I enter a bad propertyname, spring complains.. So it knows my properties.. but just doesn`t replace the property name with the property value.

            [edit]
            I even have added all the jars (14 mb) for spring, but that wont solve the problem either.

            Comment


            • #7
              It seems my bean is initialized before the property file is loaded. I have added some logging to my bean (which uses the properties). And I have added a badly named property file. In the logfile I first find the bean initialisation.. and later Spring throws an exception it can`t find the property file.

              So... how can I force spring into first loading the property file.. and than creating my bean...

              Comment


              • #8
                I have found the cause of the problem. My mail2mailpaden had jmx-metadata, so it was jmx-ified. The jmx-ification found place before the variables were replaced.

                I removed the jmx metadata and everything works fine.

                Comment

                Working...
                X