Spring placeholder not resolved - spring

I am declaring properties in applicationcontext.xml and passing external.conf as jvm argument. I am able to read the values in properties file without an issue.
<util:properties id="hbaseProperties" location="file:///${external.conf}/props/hbase.properties"></util:properties>
If i add key in property file as below, it fails to resolve the external conf and its getting printed as ${external.conf}/hdfs-site.xml
site_xml = ${external.conf}/hdfs-site.xml
I am not sure why this is not getting resolved. What should be done to make it work.

Add in your applicationcontext.xml following code snippet
<context:property-placeholder location="<pathToYourPropertyFile>/external.conf"/>
For more information about the property placeholder mechanism look at the spring documentation

Related

How to get Spring property with embedded property variable as a literal

I currently have a Spring application.properties with 2 properties defined as follows:
validator.url=http://location.com/${domain}/${family}
query.validator.url=${validator.url}
Currently my application resolves query.validator.url to be ${validator.url}
is there anyway that I can have it resolve to the same value as validator.url which is http://location.com/${domain}/${family}
Note: ${domain} and ${family} don't resolve, they are handled in code.
Try the following:
validator.url=http://location.com/#{'$'}{domain}/#{'$'}{family}
query.validator.url=${validator.url}
Both validator.url and query.validator.url will resolve to http://location.com/${domain}/${family}
Alternatively, you can create a property for the $, which you can then use inside validator.url, e.g.:
var=$
validator.url=http://location.com/${var}{domain}/${var}{family}
There's an issue regarding this on Spring's JIRA

context:property-placeholder comma-separated list of resources don't work with system property placeholder

I can't entirely configure property-placeholder from system property because I can't give comma-separated list of resources.
I'm trying to do like:
<context:property-placeholder location="${config-location}" />
I use system property to configure this. It works if I give one location only, like "classpath:main.properties", but it does not if I'm trying this: "classpath:main1.properties,classpath:main2.properties".
If I use this latter exact value directly in xml configuration it works fine. I guess it resolves comma-separation earlier than placeholders. It should be the other way around.
P.S : version 4.3.4
Another possible worth tryout would be,
<context:property-placeholder location="#{systemProperties['config-location']}" />
how to read System environment variable in Spring applicationContext

Profiling for different environment Spring MVC

I'm working on a maven Spring project, and I'm running Spring 3.0.7
in my .js file i use url for jquery ajax call like following
url : "/myProjectName/controllerName/MethodName"
In jdbc.properties file my userName & password is like following
jdbc.username=root
jdbc.password=
and some other like this. this is what i do when I work on my pc.
But before uploading my application, I have to change these as following
url : "/controllerName/MethodName"
jdbc.username=myName
jdbc.password=myPass
so what i am doing now is changing this every time manually before uploading my jar in the server.
Now I am wondering if there any way to do this so that I don't have to change this value manually every time before uploading it to the server. I read about profiling I dont know how to use it.
How to do this? Example code is highly appreciated.
You can add 'profile' attribute to your spring configuration file
Look 'Enter bean definition profiles' section at here
Use below code to set you spring profile, may be you can do this in your ServletContextListener
System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, [YOUR PROFILE]);
Load both 2 xml file below, only the file match the active profile will effect.
In develop.xml
<beans ... profile="develop">
... beans here will only be loaded while profile is 'develop'
</beans>
In server.xml
<beans ... profile="server">
... beans here will only be loaded while profile is 'server'
</beans>

Spring context:property-placeholder not loading properties

I have a Spring config file with one bean. The bean has 2 properties that are populated from a properties file. I am using the following config in my Spring file to copy the values in but it does not seem to be working.
<context:property-placeholder ignore-resource-not-found="true"
system-properties-mode="NEVER"
location="classpath:my.properties"/>
The weird thing is - this has worked before. Can anyone tell me why this would not be successful in copying the properties across?
I know the infomation given is scant. I'll add elaborate if needs be.
Try the classpath*: prefix. And try giving the relative path to the conf file, and make sure it is really on the classpath (note that WEB-INF is not on the classpath - the classpath of a webapp starts at WEB-INf/classes (and lib))

Spring application-context, not able to load (property-placeholder) .properties file

I have a web-app, which loads a application-context files from many locations.
One of the application-context file is in a .jar file (this jar is present in WEB-INF/lib).
This application-context has an entry like this:
<context:property-placeholder location="classpath:META-INF/spring/default.app.properties" ignore-unresolvable="true" ignore-resource-not-found="true"/>
But the default.app.properties is never found. I keep getting errors about
Could not resolve placeholder 'db.driver' - something that is defined in default.app.properties and referred in application-context via ${db.driver}
It is almost as if property-placeholder is being ignored. I tried giving absolute path to my default.app.properties too.. even that wouldn't work.
Have you solved this problem? I've encoutered the same recently. My solution is simple and unlikely to be the case but... in my case there were two types of placeholders of different types. One type was configured using
<context:property-placeholder/>
the other type was configured as a bean of type ServletContextPropertyPlaceholderConfigurer. Removing one type of placeholder solved the problem.
Because Spring allow exist only one <context:property-placeholder/>, When Spring find a <context:property-placeholder/>,it will ignore the remains. So put all the properties conf in one place.
reference to :http://www.iteye.com/topic/1131688

Resources