I've seen different delimeters for properties
: =
Can't find reference about it.
In application.properties you can specify properties in either of these forms:
Using =:
key=value
Using :
key: value
All examples in the Spring docs for application.properties use = as the delimiter. But Java properties also supports : so by extension Spring (which is utilising java.util.Properties under the hood also supports :. Here's the relevant extract from the java.util.Properties Javadoc ...
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator.
Related
We have a Spring boot project using the deprecated #StreamListener and now we are switching to Spring Cloud Stream functional kafka binders.
The problem is that this service connects to multiple kafka topics and our single line spring.cloud.function.definition: topicA;topicB;topicC;...;topicN is becoming very long
I would like to know how to use on Spring's application.yaml the yaml capabilities such as multi line values (such as | or > operators) but I haven't found something similar on documentation.
My goal would be something such as
spring.cloud.function.definition: | topicA;
topicB;
topicC;
...;
topicN
Thanks
There are multiple ways to represent multiline values in YAML. Details could be found in How do I break a string in YAML over multiple lines? and most of them are supported in Spring and could be used in application.yml.
Using multiline for function definitions in Spring Cloud Stream
All above approaches works in Spring but at the end it really depends how the result value is used. In some cases new line character is preserved and in some cases replaced with space.
From what I see spring.cloud.function.definition is parsed in org.springframework.cloud.stream.function.FunctionConfiguration and logic doesn't expect any white spaces.
The only approach that would work in this case is double-quoted block with escaped the newline
spring.cloud.function.definition: "topicA;topicB;topicC;\
...;topicN"
I have the following property annotated with #Value. I have a default value defined using the default separator of ':"
#Value("${prop.url:http://myurl.com}")
Is there a way to escape the ':' in http://myurl.com or do I have to define a different separator value in my configuration.
Update:
For spring 4.2 and higher, no single quotes are needed. Spring will see the first colon as special, and use all the rest as a single string value.
For spring 4.2 and higher,
#Value("${prop.url:http://myurl.com}")
For the previous versions, I believe single quotes will do the trick:
#Value("${prop.url:'http://myurl.com'}")
If you need to pass a list of Strings that contain colon with default value then do like:
#Value("${parameterName:}#{T(java.util.Arrays).asList(\"abc:1\",\"def:2\")}")
private List<String> parameters;
On Spring version 3.2 the default value works without quotes.
I am having issue with the '\' (backslash) character inside bootstrap.properties file.
I first define a variable within bootstrap.properties file:
var1=AB\AC
Then, I define a jndiEntry inside server.xml file:
<jndiEntry value="${var1}" jndiName="jndi/var1" id="var1">
When I look up jndi entry in my code, the '\' is lost. If I use double backslash, i.e., '\\', then what I get is a forward slash, i.e., 'AB/AC'.
How can I input '\' character?
Liberty will perform path normalization on all variables unless the attribute is defined as a password type. The only way to get around this currently would be to include the backslash character in server.xml rather than bootstrap.properties.
For example, in server.xml:
<jndiEntry value="${var1}\${var2}" jndiName="jndi/var1" id="var1">
bootstrap.properties:
var1=AB
var2=BC
According to Liberty documentation, path normalization is performed on all variables located in bootstrap.properties file
by replacing repeated forward and backward slashes with a single
forward slash, unless the value starts with double forward or backward
slashes, which remain unchanged.
However, according to their "Best practices" section:
If you need to set the value of a variable to contain repeated forward
slashes, as are sometimes used for JDBC driver connection URLs, break
the value into two parts at the double slashes. By placing the double
forward slashes as the initial characters, normalization is avoided.
For example, to store the value "jdbc:db2://host_name.com", use two
variables:
URL_PART_1="jdbc:db2:"
URL_PART_2="//host_name.com"
I am confused here:
in the documentation on the properties section there is an example on how to modify stuff in
application.properties
spring.main.web_environment=false
spring.main.show_banner=false
Note that the above uses the "=" sign to apply values to the properties
In the spring-boot documentation in the logging section there is an example on how to modify logg levels
ogging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
Note that the above uses the ":" sign to apply values to the properties
What is the correct way to set properties? If you test it both ways works!
Is there a typo in the docs and the second example is actually for application.yaml?
From the javadoc of java.util.Properties:
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator.
I'm hoping there is a very quick simple answer to this, I've read a few other questions on here that reference SpEL and escape sequences, but still haven't succeeded.
I would like to split a property into a list of String using #Value and SpEL. The property will be:
12345|12345|12345
So I need to split on pipe characters, I can do this in Java by using .split(\\|) to successfully escape the pipe character. I have tried with no slashes, 2 slashes and 4 slashes and all are unsuccessful. Is it possible to split on pipes using the following code? My client wants to keep using pipes for whatever reason...
#Value("#{'${list.of.blocked.people}'.split('\\|')}")
private List<String> myBlockedPeopleList;
Thanks.
Here is a link to the question that got me this far, for reference.
Reading a list from properties file and load with Spring annotation value
I also tried looking at the Spring Docs, but I couldn't find any reference to escape pipes in their documentatin.
Spring Documentation on SpEL
Found the answer on a web development forum here.
My solution was to basically give up on trying to escape the pipe character with back-slashes and use the unicode character escaped sequence. If anyone else is trying to pull in a pipe-separated String using #Value, the following code is tested and works with Java 6, Tomcat 6 and Spring 3.
// Reads blockPeopleString delimited with | and splits into List of Strings
#Value("#{'${blockPeopleString}'.split('\\u007c')}")
private List<String> blockPeopleList;