I have a Spring Project where I am using bean configuration file
beans.xml.Inside the bean Configuration file, i have defined some properties for a PlaceHolder which refers to classPath...While the application is running, the properties are getting loaded from /unknownPath/Dev/Loc1/System.properties
Where
${BUS_ENV}=Dev
${LOCATION1}=Loc1
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>classpath:${BUS_ENV}/lbsprocessor.properties</value>
</list>
</property>
<!-- Force system properties to override any deployed runtime properties -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
I didn't specify classpath while running my project in IDE
I don't have those files in my resource folder
There are around 65 such files exists(for various reasons) as Dev/Loc1/System.properties
I am not able to find from which location the properties are getting referred. Even after debugging, I couldn't find out what classpath refers to. Please help me with figuring out
If you are using eclipse IDE right click on your project select properties then select Java Build Path. On first tab Source there is one input named Default Output folder that value is your classpath. Check all your properties files are there in that path.
Referring to your point 2 problems might be in these line
<value>classpath:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>classpath:${BUS_ENV}/lbsprocessor.properties</value>
You are using classpath for file location which means these properties file have to be in the .war file at /Dev/Loc1/System.properties
If properties files are outside of project may be at system level you can access them like this
<value>file:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>file:${BUS_ENV}/lbsprocessor.properties</value>
eg:
<value>file:/home/testuser/system.properties</value>
I am using Mac OS ,in this we are storing the Configurations as a jar file under
/Library/Java/Extension.So java is directly referring classpath to that location by default.
Related
I currently have a file, {project-name}/WebContent/WEB-INF/applicationContext.xml, that sets a sub directory into a bean file for further use.
<bean ....>
<property ...>
<value>WEB-INF/utilities</value>
</property>
</bean>
Changes are being made to have these files normally located in this sub directory added to the Java resource folder when the war is build (done with the pom file). I have tried setting the bean to use this path, but failing when trying to access files inside of it.
<bean ....>
<property ...>
<value>{project-name}/Java Resources/src</value>
</property>
</bean>
Is there a better option to reference the project path so applicationContext.xml can have visibility to the source folder?
Can the pom declare an alternate destination for resource files when building? If I can have the pom set the files to "WEB-INF/utilities" instead of defaulting to "Java Resources/src" that should be able to resolve my issue.
I have read questions regarding accessing files from the java resource directory, but those solutions require implementing java classes to read values from the included files; not setting the actual path location in a bean for use by other beans. Not sure the benefit of having a new class just to retrieve a directory name.
I have developed a webapplication which is having jsp and java code. Right now I have placed all the key-value into a env/lifecycle specific properties file (like conf-dev.properties,conf-stg.properties,conf-prod.properties).
I want to externalize these properties file so that it can be placed outside of war(without effecting the war).
right now war file is tightly coupled with properties file. if i have to modify any thing i have to build and make war and deploy.
I have very limited access on deployment server machine (only have access for one folder where i can put my configuration files) & deployment process is handled by CI(jenkin & automated script).
I explored on internet and came to know that we can achieve this using spring, would like to know what is the best way to achieve this?
As you are using Spring I suppose you already use PropertyPlaceholderConfigurer. If not you should ;)
The location of a property file can be anything that can be resolved as spring Resource. This includes classpath, servletcontext and also file references as URIs (file:///... For absolute paths)
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="${config.file.location}" />
</bean>
If I understand your question, then you can use Class.getResourceAsStream(String) the linked Javadoc says (in part)
This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).
The better way to externalize env specific properties is to use "user.home" or "user.dir".
Thanks #Martin F..
Resolved:This is the final one i used and its working fine in dev,stage Env.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="order" value="1"/>
<property name="locations">
<list>
<value>classpath:conf-${cisco.life}.properties</value>
<value>file:///${openshift.home}/data/conf-${cisco.life}.properties</value>
<value>file:${openshift.home}/data/conf-${cisco.life}.properties</value>
</list>
</property>
</bean>.
and i used script action hook in openshift to set the lifecycle on system level.
appname=echo $OPENSHIFT_APP_NAME
case "$appname" in
*dev)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=dev";
echo "setting up env life dev for " $appname
;;
*stage)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=stg;
echo "setting up env life as stg for " $appname.
I made a message.validation.properties file inside root/src/resources and my code below inside the spring-servlet.xml is not recognized.
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>src/resources/messages.validation</value>
</list>
</property>
</bean>
I have tried classpath:messages.validation, messages.validation but I only get the warning message
WARNING: ResourceBundle [src/resources/messages.validation] not found for MessageSource: Can't find bundle for base name src/resources/messages.validation, locale en_US
How can I locate this file? Note that I do not want to put it in another folder ie. WEB-INF/classes.
This depends on how you build your project, but in the end the message.validation.properties file should up on the classpath, ie. WEB-INF/classes. If it is directly in there, you would specify
<value>classpath:messages.validation</value>
I'll assume that your src folder is a source folder in your IDE. As such, anything under it will be compiled/copied so that it ends up in the classpath directory. As such, you should be using
<value>classpath:resources/messages.validation</value>
Again, if /src/resources is the source folder, then you need
<value>classpath:messages.validation</value>
You have to add the resource folder to the classpath so it can become accessible.
If you use Eclipse, right-click on project, select Properties. Then on the left side select "Java build path" and add you resource directory with "Add Class Folder".
Try this:
<value>classpath:messages.validation</value>
As far as I have found, Maven cannot change the current working directory for every module build in a reactor build (to the module that is being built) because it is not possible in standard Java to change the current working directory. First of all, is this correct?
If yes, how can I make the following scenario work?
UPDATE: I have a some.properties file that is supposed to be on the classpath of the module being built (e.g. in src/main/resources) and it is being referred to in a Spring context file of a Maven Plugin, like this:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<array>
<value>classpath:some.properties</value>
</array>
</property>
</bean>
The Maven Plugin is being executed in "process-test-resources" phase. Also, some.properties file has a property that refers to an external relative configurable path:
some.spring.config.dir=target/test-classes
Then, a few config files from this path are loaded in spring context files in the module being built, such as:
<property name="configLocation" value="file:${some.spring.config.dir}/cache.xml" />
This scenario works when I build the project from its pom.xml directory but not when I build it as part of a reactor build from a top level directory. Is there a way to get it working when running maven from any directory and also keep the path in my properties file (e.g. "target/test-classes") relative?
Two steps required:
Include resource filtering for configuration file: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
in configuration file add basedir:
some.spring.config.dir=${basedir}/target/test-classes
In "target" directory "some.spring.config.dir" will be filled with full path.
I have a properties file which sits on APPSERVERS HOME directory(JBOSS_HOME/PROJECT_PROPERTIES/abc.properties).PROJECT_PROPERTIES is the directory where we are keeping all the project related property files.I need to read this properties file from spring config.Earlier i was using the following approach.
<bean id="propertyOverrideConfigurer"
class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location" value="classpath:abc.properties" />
Now we moved all our properties file to JBOSS_HOME/PROJECT_PROPERTIES directory.
Please provide me some pointers how to access the properties file using spring.
This should help you out:
How to read properties in spring
You can pass the properties file path as a command line argument
<property name="location" value="${propertiesFile}" />
Then for the jvm parameters pass
-DpropertiesFile=my/configuration/file.properties