<context:property-placeholder loading from the filesystem - spring

<context:property-placeholder ignore-unresolvable="true" location="classpath:test.properties" />
Above mentioned way of loading the property files from contex:property-placeholder works just fine. What I want to do is loading the property file from the file system.
Let's say I have the file in C drive C:\spring\test.properties. Here how can I load the file using context:property-placeholder.

Use a file: URL instead of a classpath: one
<context:property-placeholder ignore-unresolvable="true"
location="file:/C:/spring/test.properties" />

Give properties to web application(Spring based) from filesystem(External Location)
1) specify placeholder in applications root xml as
2)define enviornment name as "config" in server's context.xml and give path of file
spring.xml
file:#{contextJndi.lookup('java:comp/env/config')}
context.xml
Environment name="config" override="false" type="java.lang.String" value="D:\config.properties"/>

Related

Properties file with Spring - how to use them

I have a properties file sample.properties put in project's root resource folder with this content:
auditorium.name=New York.
Now in spring.xml I try to access some of them with following syntax:
<bean id="auditorium1" class="hometask.domain.Auditorium">
<property name="name" value="${auditorium.name}" />
</bean>
But in run-time this property is not resolved. name is set to string ${auditorium.name} instead of New York.
Is there anything else I should set up to have it working?
you should set-up the placeholder of your properties this way:
<context:property-placeholder location="classpath:sample.properties"/>

PropertyPlaceholderConfigurer - Can it load all files from given directory path?

Does the Spring PropertyPlaceholderConfigurer take a directory location and load all properties file, without me having to specify the file names explicitly.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="<path to all properties files>" />
</bean>
Is there any other way to achieve this?
You can do something like this
<context:property-placeholder location="classpath:file1.properties,classpath*:project-common.properties,classpath*:project-${spring.profiles.active}.properties"/>
In my case, it was a legacy system so property files didn't have some standard names but for sure you can use wildcards to reference your property files.
<context:property-placeholder location="classpath:*.properties"/>

How to locate the properties file in the spring context configuration file

I am using spring web mvc project, and I put all the spring related files under WEB-INF\spring, including a ormlite.xml and a jdbc.properties.
Now I want to locate the jdbc.properties file in the ormlite.xml,Like this:
<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>
But when I run the application ,it will told me that :
Could not load properties
It can not find the properties file.
What is the problem?
From Spring forum:
The problem is that /WEB-INF isn't accessible as it isn't in the root
of the path, you must use the same path as you use in your test case
(include the src/main/webapp part but that will break your application
from running).
I suggest you move the jdbc.properties to the src/main/resources
directory and simply use classpath: prefix to load the properties.
Code:
<context:property-placeholder location="classpath:jdbc.properties"/>
The code above assumes they are on the root of the classpath (which is
where they are when they are in src/main/resources).
I hope this can help someone else.
I had the same problem - property files outside the classpath.
My solution:
First define a properties bean:
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/your.properties</value>
</property>
</bean>
Then reference it in the property-placeholder:
<context:property-placeholder properties-ref="configProperties" />
Works perfectly well for me!
Instead of:
<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>
Use:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/spring/jdbc.properties"/>
And your properties will be available in Spring file, and don't forget to add a namespace: xmlns:p="http://www.springframework.org/schema/p"
I think you're missing the prefix to instruct Spring how to attempt to load the properties. I think your definition needs to be:
<context:property-placeholder location="file:/WEB-INF/spring/jdbc.properties"/>
Note the addition of the file: prefix.

Tomcat Context-Params ignored in Spring webapp when using PropertyPlaceholder

I was previously using, the now deprecated, class org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer to load a properties file from the server's filesystem. I had the following bean definied:
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="locations" value="${config}"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="false"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="searchSystemEnvironment" value="false"/>
</bean>
The config is an argument that is passed when starting Tomcat, i.e.
-Dconfig=/path/to/application.properties
For the webapp I also have a context file:
<Context docBase="/path/to/application.war">
<Parameter name="host" value="localhost" override="false"/>
<Parameter name="port" value="8080" override="false"/>
</Context>
If the .properties file, specified by the -Dconfig argument, contains the property that some other bean references then the value from the .properties file is used, otherwise the value from the the context xml file is used.
This allowed me to have a set of default properties deployed with the WAR and if required, I was able to specify a .properties file to override particular values.
Now, I'm updating to use the new property abstractions in Spring 3.1 but I can't seem to figure out what the equivalent approach to this is?
I have the same context file and war deployed in the same way, and I now have the following in the application:
<context:property-placeholder
location="${config}"
system-properties-mode="OVERRIDE"
ignore-resource-not-found="true"
ignore-unresolvable="true"/>
This finds and uses the properties from the properties file, BUT it does not use the values from the context XML file.
How do I get my application to use the context params when using this new property-placeholder?
Thanks.
To summarise the problem is that the Context Parameters from the servlet context file were not being used to resolve placeholders when using the new Property Placeholder namespace introduced in Spring 3.1.
I have figured out a solution, with the following
<context:property-placeholder location="${config}" local-override="true" ignore-resource-not-found="true"/>
I can specify one or more *.properties files on the local filesystem using a JVM arg, eg:
-Dconfig=/path/app.properties
If a placeholder property cannot be resolved after checking the app.properties file then the Servlet Context Parameters are checked.
This allows me to have default values using context params in a the web.xml and where I need to I can override these values by specifying the location of *.properties files using the config JVM arg.
The key to getting it to work this way was to include local-override="true", which is false by default. I'm not fully sure that it makes sense, since the description for that attribute is:
Specifies whether local properties override properties from files. Default
is "false": Properties from files override local defaults.
If the same property key exists in the app.properties and the web.xml the value from the app.properties is used.
Spring uses a default property file unless the user-based property file is defined. If you want to control .properties file, please follow the instructions posted here.
If you want to take advantage of application.properties there are two ways to do it.
<!-- allows for ${} replacement in the spring xml configuration from the
system.properties file on the classpath -->
<util:properties id="appProperties" location="classpath:application.properties"/>
<context:property-placeholder location="classpath:application.properties"/>
util tag lets you to use a Property class to read the properties across your application. For example:
#Autowired
public MyPropertyReader(Properties appProperties) {
String prop1 = appProperties.getProperty("my.address");
String prop2 = appProperties.getProperty("my.version");
}
If you want to use the values within your context file use the context:property-placeholder tag. Then you can use your values as
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="${jms.primary.server}"/>
where for example jms.primary.server=172.168.10.18:6161 in application.properties.

Spring properties file as xml

I have properties file config.properties where are stored some application wide properties. And I have imported it using property placeholder:
<context:property-placeholder location="classpath:/config.properties" />
I need to store properties in XML file to pass some XML schema validations.
My question is how to import XML file as properties file in spring,?
Thanks,
Arsen
PropertyPlaceholderConfigurer already supports xml property files via the DefaultPropertiesPersister
The xml file format for the properties is as below.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="key1">Value 1</entry>
<entry key="key2">Value 2</entry>
</properties>
you can use
<context:property-placeholder
location="classpath:/com/myProject/spring_prop.xml" />
<bean id="bean" class="org.MyBean">
<property name="key1" value="${key1}" />
</bean>
In addition to the other answer here, I have also seen xml properties loaded directly as named properties files:
The spring file contains:
<util:properties id="myXmlProps" location="classpath:/com/myProject/spring_prop.xml" />
This can then be accessed via springs expression language as:
"#{myXmlProps['key1']}"
And injected into Strings in classes with:
#Value("#{myXmlProps['key1']}")
private String aValueForKey1;

Resources