0

I have this in my appContext.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>file:pathTo/service.properties</value>
            <value>file:pathTo/configuration.properties</value>
        </list>
    </property> 
</bean>

And I'm setting a string with

@Value("${myServiceKey}")
private String url;

That WORKS and I get the value of myServiceKey in the url.

But I want to use a default value when myServiceKey is not present, so I tried this

@Value("${myServiceKey:defaultValue}")
private String url;

and it always sets the "defaultValue" instead of the correct one "myServiceKey".

I also realized that using this:

@Value("#{systemProperties['myServiceKey']}")
private String url;

I have an exception

WARN  MSF4JMessageProcessor:262 - Unmapped exception -java.lang.IllegalArgumentException: URI must not be null

Is that related? What's wrong??

I'm using spring version 4.3.9.RELEASE

Thanks in advance.

marysol
  • 11
  • 3

2 Answers2

1

Finally I found the problem and it was that I had multiple properties in the PropertyPlaceholderConfigurer. I found it in: https://jira.spring.io/browse/SPR-9989

And the solution it worked for me was one suggested here Spring Boot : Spring always assigns default value to property despite of it being present in .properties file

I separated the PropertyPlaceholderConfigurers in two different ones and added to one of them the property valueSeparator

<property name="valueSeparator" value="="/>

Then I set the default value like this:

@Value("${myServiceKey= H3ll0W0rld}")

Now it works. Hope it helps someone else.

marysol
  • 11
  • 3
0

I don't think that the MSF4JMessageProcessor:262 - Unmapped exception -java.lang.IllegalArgumentException: URI must not be null is related to your problem.

We have been using the default value without any problem. Just tried to set some example up

mail.properties

smtp.port=587
myServiceKey=s3cr3t
user.dir=D:\Java

Controller class :

@Controller
public class HelloWorldController {

   @Value("${myServiceKey: H3ll0W0rld}")
    private String myServiceKey;
    ...
   @RequestMapping("/index")
   public String index(){
     System.out.println("myServiceKey = "+myServiceKey);
     ... 
   }
}

This code correctly prints myServiceKey =s3cr3t. When I update my mail.properties as follows,

smtp.port=587
user.dir=D:\Java

It prints myServiceKey =H3ll0W0rld

amdg
  • 2,211
  • 1
  • 14
  • 25
  • Hi, thanks for your answer. I know, it should work. But as I said in the first part of my question it's not taking the correct value when ":" is present. It takes "H3ll0W0rld" instead. The thing is that it works correctly when I don't try to set a default value... It could be a problem with how the properties files are set?? – marysol Feb 09 '18 at 19:54