Spring Property Placeholders with String Concatenation
Solution 1
I solved the issue by changing PropertyPlaceholderConfigurer
beans to Properties
.
<util:properties/>
are accessible in SpEL.
Example:
"#{prop[host+'.'+'maxLength']}"
where host
is a string bean.
Solution 2
To concatenate the values parsed from Spring property placeholders, you need to escape their values using single quoutes (''
) and wrap the placeholder expressions by a SpEL expression using #{}
.
<bean id="myService" class=""com.services.MyService">
...
<property name="endpointAddress" value="#{'${server}' + ':' + '${port}' + '${endpoint}'}" />
</bean>
where:
server = http://domain.host.com
port = 7777
endpoint = /services/myservice
The result would be:
Solution 3
It would be better to have environment specific properties in a file of its own and use Spring Profiles. For example, I have four xml files just for db configuration, local.db.xml, dev.db.xml, qa.db.xml and prod.db.xml. Inside each db.xml, I set the profile to the appropriate value.
My local.db.xml has
<beans profile="db.local" .. >
For starting Tomcat, I specify the VM options as follows
-Dspring.profiles.active=db.local
user2235506
Updated on July 16, 2022Comments
-
user2235506 almost 2 years
My problem looks simple but I'm not able to resolve it. I have a properties file which contains configuration details of all environments (dev, qa, prod).
Example
config.properties
:dev.maxLength=2000 qa.maxLength=4000
We have a parent Properties file which holds the host name, environment mappings.
Example
hosts.properties
:host1=dev host2=qa
The property name
host1
is stored in a beanhostname
.<bean id="hostname" factory-bean="localhostInetAddress" factory-method="getHostName"/>
To resolve the config properties name I have to join the strings as follows,
${${**hostname**}.maxLength}
which should be resolved as${dev.maxLength}
I tried using SpEL with no success. I am getting
Could not resolve placeholder
Exception. How can I concatenate a bean value in property place holder? How are dynamic property names constructed?Spring version 3.2
-
user2235506 about 11 yearsI cannot use Environment variables/JVM properties, it is a policy constraint.
-
Vu Nguyen over 6 yearsMind to share your XML? I' want to do the same thing but wasn't sure of what you mentioned by changing it to Properties...