Reference Spring properties file using path relative to config file - java

I am moving properties from inside my Spring config file to a separate properties file. This is included in the config file with
<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
<property name="location" value="file:properties/${CONFIG_MODE}/service.properties" />
</bean>
As it stands, the location of the properties file is relative to the current working directory of the server process.
This creates the requirement that the process must be started from a specific working directory, and even worse allows for the (admittedly remote) possibility that it could pick up an entirely different properties file - for example if it was started with the working directory set to an older version of the service.
I'd like to reference the properties file using a path that is relative to the directory containing the config file.
Looking at FileSystemResource, it seems createRelative might be what I need, but I can't figure out how to use it in the config file.
Thanks,
Steve

I don't know of a way to do that.
What you can do, however, is load the properties file from the classpath:
<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
<property name="location" value="classpath:path/to/service.properties" />
</bean>
The classpath location of your properties file is a far more predictable situation, and it'll work as long as your classpath is set up properly.

Using 3.1, you can keep the files off of the classpath if you want.
With the following bean definition,
<bean class=
"org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
<property name="location"
value="file:${props.path}/service.properties" />
</bean>
you can set a property using the java command line
java ... -Dprops.path=path/to/where/it/is

Supposing you have placed the config.properties file inside WEB-INF
Then:
<bean id="propertyConfigurerInternal"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:../config.properties</value>
</property>

Related

Maven using external directory as config dir

I'm using maven to generate a jar with all dependencies inside the jar (as shown in
this question). This is working fine. The problem is, I need to have some .properties file un an external directory (like C:/program/config) where there will be some configurations parameters. These parameters are used in Spring:
<property name="driverClassName" value="${database.driver}" />
How can I add this directory to the classpath so that Spring (or the java code whenever it neets to) can access to the files under C:/program/config.
Note that I don't wan't to include the files inside the jar, I wan't that the classpath recognize this directory (I know I can do it in a cmd with set CLASSPATH="", but I neet to avoid this).
Thanks.
Configure the spring.xml like.Put database properties in resource floder
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<property name="driverClassName">
<value>${DRIVER}</value>
</property>
Obviously the simplest solution will be to include explictly c:\program\config into the classpath, but if you want to avoid it, I just think of another alternative: You need to include the files c:\program\config\*.properties into a new Maven library, and then set it as a system dependency of your code:
- myproject
/pom.xml -> has a dependency on my_config_files
/src/java/main -> source code
- my_config_files
/pom.xml -> includes c:\program\config as a resource directory
In this way, you'll end up with two libraries: One for the code, and the other just for configuration files.

How to access jdbc properties file (for Spring Security configuration) which is outside WEB-INF folder?

How can we access jdbc properties in Spring Security configuration which are outside WEB-INF folder of the webapp? My project hierarchy is like below:
tomcat
webapps
appName
Configuration
jdbc.properties
other configuration files too are here
WebPages
all the jsps/htmls here
WEB-INF
web.xml
spring-security.xml
classes/
lib/
tlds/
Inside spring-security.xml I am trying to refer jdbc.properties and everytime I get file not found exception,
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${catalina.home}/webapps/appName/configuration/jdbc.properties"/>
<property name="location" value="file:${catalina.base}/webapps/appName/configuration/jdbc.properties"/>
<property name="location" value="/configuration/jdbc.properties"/>
<property name="location" value="file:/configuration/jdbc.properties"/>
<property name="location" value="classpath:/configuration/jdbc.properties"/>
<property name="location" value="classpath:/configuration/jdbc.properties"/>
</bean>
{catalina.home} and {catalina.base} work but only till the server restart or application redeploy happens. Almost all the answers here on SO or on internet point to solution where jdbc.properties file is placed in classpath (i.e inside classes folder) however I can't do that in my project due to internal reasons.
If you move your properties file to src/main/resources, assuming your project is managed by maven, then you could retrieve it by doing
Properties prop = new Properties();
try {
// load a properties file
prop.load(YourClass.class.getResourceAsStream("/jdbc.properties")); // note the leading /
System.out.println(prop.getProperty("password"));
} catch (IOException ex) {
ex.printStackTrace();
}
where YourClass is whatever class this code is in.
Maven places the class files of your compiled classes and all resources in src/main/resources in WEB-INF/classes where only your application can access them.
If you put the file in src/main/resources/someFolder, you'll need to access it from
prop.load(YourClass.class.getResourceAsStream("/someFolder/jdbcProperties"));
The path you provide to the above method is relative to the package of the class you are in, unless you specify a leading forward-slash, in which case it will be relative to the root of the classpath, ie classes folder.

Accessing file system resource in <resource mapping="" location=""

I have a Spring application where I want to take my static resources from a simple file system (D:/.../..).
Could I do something like
mvc:resources mapping="/resources/**" location="d:/../.. .css, ...js"
Or if there is any other way I could achieve this.
However, I need to do this only in Spring configuration file.
Yes
You can read files from file system like
Resource resource = appContext.getResource("file:c:\\testing.txt");.
Please refer full example from MkYong here. And if you are creating web application then you should load appContext like:
#Autowired
private ApplicationContext appContext;
You can use PropertyPlaceholderConfigurer to read your properties file from either classpath or WEB-INF or from file system adding below configuration to your applicationContext.xml file:
A. File in Classpath:
<bean id="dbProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties" />
</bean>
The keys defined in the db.properties file can be read using ${key name}.
Example: Consider, your properties file contain
jdbc.url=jdbc:mysql://localhost:3306/dbName
then you can read this property using below command
<property name="url" value="${jdbc.url}" />
B. File in WEB-INF:
When your properties file in under WEB-INF directory, then you just add below code snippet under dbProperties bean & it'll starts looking your properties file in the WEB-INF directory of the application.
<property name="location" value="WEB-INF/db.properties" />
C. File on File System:
When you want to read file from file system then just add below code in dbProperties bean.
<property name="location" value="file:///D:/database/db.properties" />
NOTE: You can either use file:/// or file: in case C.
Hope this solves your problem.
Resource res=new ClassPathResource("Yourfile");
you can read the file as it is.

Cannot Open properites file in Spring Web application

I'm trying to set up a couple webapps on tomcat but none of the properties files are getting picked up
2014-02-19 15:47:02,106 - WARN org.springframework.core.io.support.PropertiesLoaderSupport - Could not load properties from class
path resource [indexing.properties]: class path resource [indexing.properties] cannot be opened because it does not exist
2014-02-19 15:47:02,110 - WARN org.springframework.core.io.support.PropertiesLoaderSupport - Could not load properties from class
path resource [user-service.properties]: class path resource [user-service.properties] cannot be opened because it does not exist
2014-02-19 15:47:05,169 - WARN com.cubeia.backoffice.users.Configuration - No user-service.properties configuration file found. U
sing default.
The /WEB-INF/classes/service.xml file has the following definition:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:default-indexing.properties</value>
<value>classpath:indexing.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true" />
</bean>
I've tried copying the .property files everywhere i could think of. tomcat/conf, tomcat/lib, tomcat/conf/Catalina, tomcat/conf/catalina/localhost, webapps//WEB-INF/, webapps//WEB-INF/classes/, then also in the same directory with the actual .class files, temp directory, you name it. Just won't pick it up.
Later edit: I have also tried values like /WEB-INF/classes, WEB-INF/, tomcat/conf, tomcat/lib, classpath*:... all possible combinations basically.
You should put your properties file in the src/main/resources directory of your webapp. Maven (I'm assuming you're using maven) will then copy this file into your WEB-INF/classes directory for you, which puts it on your classpath. If you then follow the answer from #arahant everything will be fine.
The classpath: points to WEB-INF/classes
If you are using Spring MVC 3, you can use the util:properties annotation. The following is an excerpt from a working configuration.
<util:properties id="emailProperties" location="classpath:../email.properties"/>
Here my email.properties is in the WEB-INF directory and hence the ../ before the file name
The util schema is present at http://www.springframework.org/schema/util/spring-util-3.0.xsd

Java, Spring, Unable to find /WEB-INF/spring.properties do I need to set it somewhere besides propertyConfigurer?

I am getting an error message that Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring.properties] cannot be opened because it does not exist. The spring.properties files does exist and is in my /WEB-INF directory (I have confirmed that it is in my build directory after building the project). I have it set on my project's .classpath directory like this:
<classpathentry kind="src" path="src/main/webapp/WEB-INF/spring.properties"/>
In my Spring application context, I have it entered like this:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/spring.properties" />
</bean>
I apologize if this is a basic question but I really am perplexed as to what the problem is and how to solve it, I have done a lot of searching on this and can't seem to figure it out. Thanks for any advice
Looking at one of my webapps that uses a PropertyPlaceholderConfigurer, I see that I put the properties in /WEB-INF/classes and then configured the PPC to use it with a Spring classpath: URL; i.e.
/WEB-INF/classes/substitution.properties
accessed as
classpath:substitution.properties
Spring supports a ServletContextResource, which you can use by leaving off the resource prefix entirely. "You will get back a Resource type that is appropriate to that particular application context", and since we are using a web context, that resource will be a ServletContextResource.
The path is from the root of your webapp. Our paths look like
<context:property-placeholder location="/WEB-INF/spring/appServlet/application.properties"/>
Your path ("src/main/webapp") suggests you are using Maven to build the project. If this is the case, put your .properties -file(s) to /src/main/resources and use "classpath:<filename>" to access them, everything under src/main/resources should be accessible through classpath without any further configuration.
Try putting the file under WEB-INF/classes/ and referring to it with value="spring.properties". I think that should do the trick.
Just put the spring.properties file under the directory src/main/webapp (alongside WEB-INF) and referring to it with
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>spring.properties</value>
</list>
</property>
</bean>

Categories