Read properties file from included jar - java

I am trying to create Annotation parser that reads properties file. The structure looks like this:
- annotationParserModule
- declaration of interace
- annotationParser
now my app has structurelike this:
- main
- java
- resources
- .properties file
I am including annotationParserModule via maven, so if i a not mistaken, jar fire is inlcuded in my project.
However i need to read properties file outside of the jar provided by annotationParserModule, is somethig like that possible? If so, what is the right way to achieve this?
Thanks for help.

If the resource you want to read are included in the classpath of your application (eg. inside a jar, in the WEB-INF/lib folder, a folder passed with -cp to java command, etc..) you can read the file using the getResourceAsStream method of the Class class.
Providing an example for your case, having folders like:
- main
- java
- resources
- yourconfig.properties
In your YourAnnotationParser code you can have:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("yourconfig.properties");
If the file is nested:
- main
- java
- resources
- iamafolder
- yourconfig.properties
So:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("iamafolder/yourconfig.properties");
BTW this is a very simplistic response, the code works on common scenario, what it does behind the scenes is more complex than "read a file outside a jar", but to understand it fully you need to look for how the java class loader and the classpath work.
If the file is outside the java classpath you can use the File class or the URL one to read contents.

In you applicationcontext.xml you can add the bean to read the properties file from the jar
<bean id="com.mybean"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/resource.properties</value>
</list>
</property>
</bean>
and this id will be a reference to another bean
<bean id="com.mybean1">
class="com.dependent.jar.classname">
<property name="properties" ref="com.mybean" />
</bean>

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 load Spring beans xml configuration from different location in Debug and Release mode

I am using Spring and Hibernate with a non-web application. I am creating the sessionFactory bean in the /src/main/resources/Hibernate.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
....login, password, etc
It is okay, but when I finish some changes I want to create an executable jar file and put it into another server where is different database located. The problem is that I have to open the jar file and change database configuration everytime I create that jar file to make it compatible with external database on the different server.
The solution for this problem would be to load Hibernate.xml from the outside of the jar file.
And here is the question - how to do that?
If all that is different between the two environments is the database address and credentials, I'd recommend putting those into a property file and using placeholders in your XML configuration (docs).
You can then either access the property file as classpath resource (In Spring config, use classpath: prefix for the file path) that you can configure at runtime, or let your build system copy a different version of the file into the JAR depending on target environment (in Maven, this can be done using Assembly plugin).
The solution was to add "file:" prefix.:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:config/hibernate.properties" />
</bean>

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>

Spring WebContent Resources - Access outside ServletContext

I have a Spring Web MVC application that I'd like to serve a large, partially generated file.
I've added that file to my WebContent directory and all works fine there. However, I'd also like to access that file from my various build/deploy scripts, which read and parse the file.
My current approach is to keep a copy of the file under the src directory as well as the WebContent directory. When serving the file from the web, it uses WebContent.
When serving the file for the build scripts, it uses the following spring config:
<bean id="ringCodeData" class="com.myapp.data.RingCodeData">
<property name="rulesInputFile" value="classpath:resources/rules_copy.xml" />
<!-- <property name="rulesInputFile" value="classpath:../WebContent/rules.xml" /> -->
<!-- <property name="rulesInputFile" value="file:/WebContent/rules.xml" /> -->
</bean>
As you can see, I've tried several different approaches to get the two to refer to the same file (without resorting to copies).
File paths don't seem to work since they're based on the current directory, that changes based on whether I call a given utility class from Eclipse or from the build scripts.
How can I get these to refer to the same file?
The only other thought I have at the moment is to try to setup Spring MVC to stream the file from the classpath directory.
Your best bet is likely placing it in /WEB-INF/classes (or, if you're using an IDE, just the project's src/source folder) and use <jsp:include> to include it.
<jsp:include page="/WEB-INF/classes/resources/rules_copy.xml" />

Categories