5

I am trying to load a config.proprties file data in a Spring @Configuration java class using @PropertySource and Environment variable.

Example: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html Issue is, I have a property which has value like:

     serverName = abc\xyz

When I read this property by using method,

     String server= env.getProprty("serverName");
     System.out.print(server);

Value is printed as "abcxyz".

Please note that, I tried using double backslash like,

     serverName = abc\\xyz

but still it is simply ignoring \ from the value string. Also I can not use forward slash in place of backslash.

Can you help me in fixing it? Thanks in advance!!

Sanjeev
  • 425
  • 10
  • 17
  • 1
    Maybe `/` - the normal slash - will do, as Windows often is POSIX compliable. – Joop Eggen Jun 16 '16 at 12:03
  • 1
    In property files a \ needs to be escaped \\ (see https://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0204propertiesfileformat01.html) in property files. The fact that it isn't printed doesn't have to mean it isn't there, it can by printing be interpreted as a escape char again. And as `\x` isn't anything (afaik) it will remove it. – M. Deinum Jun 16 '16 at 12:18
  • No it is not reading \ for sure as I am getting exception while processing d value further – Sanjeev Jun 16 '16 at 12:33
  • @Sanjeev Did you find a solution to this problem? – user1766169 Aug 08 '16 at 12:09
  • It is still a open question. – Sanjeev Oct 13 '16 at 10:12

3 Answers3

1

Instead of backward slash, I stored them with forward slash in config file.

While reading, I replaced them with double backward slash.

SourcePath=C:/Users/Barani/Documents/SampleData/MyInputFile.txt

    Path currentRelativePath = Paths.get("");
    String filePath = currentRelativePath.toAbsolutePath().toString() + "/config.properties";
    Properties props = new Properties();
    FileInputStream fis = new FileInputStream(filePath);
    props.load(fis);
    sourcePath = props.getProperty("SourcePath").replace("/", "\\\\");

This worked correctly for me.

Barani
  • 48
  • 4
0

This is a real ugly hack, but you can try and use unicode escape sequence for the symbol "\" which is "\u005c" so instead of string value "abc\xyz" use "abc\u005cxyz". But then again it will translate it to "abc\xyz" and then consider "\" as a start of escape symbol. So if first one doesn't work you can try to replace "abc\\xyz" with "abc\u005c\u005cxyz". See if the first or second option works for you. But in truth, I am surprised that simple escaping "\\" didn't solve your problem. Also if all fails try this "abc\\\\xyz" - this is double escaping.

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

I used spring 3.1.4-RELEASE and it worked if values in properties file contains '\\'. Like serverName = abc\\xyz

package com.test;   

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration;    
import org.springframework.context.annotation.PropertySource;   
import org.springframework.core.env.Environment;    

@Configuration  
@PropertySource("app.properties")   
public class AppConfig {    

    @Autowired  
    Environment env;

    @Bean   
    public String myBean() {    
        System.out.println(env.getProperty("serverName"));
        return new String(env.getProperty("serverName"));   
    }   
}