I need to be able to read an ehcache configuration file ( ehcache.xml) from outside the classpath to be able to have differents files by environment (to be able for example to change the multicast adress for cache sharing).
Before I was simply using an XML defined bean :
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="#{
#PreferenceService.getEhcacheFileName() }" />
</bean>
PreferenceService.getEhcacheFileName() send back a path in a properties file.
if the propertie is filled with a classpath path (classpath:ehcache.xml), the application work properly.
But if I want to use an absolute path (/home/foo/ehcache.xml) the resource is not found.
Is it possible to use an absolute path ? And if yes what properties do I need to use ?
configLocation can contain an absolute path without any trouble.
I just forgot to add the file: in front of the path in my properties file.
So my path is now : "file:/foo/foo/ehcache.xml"
Related
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.
I have the following problem. I have a jar archive with a spring application inside (pure spring with xml configuration, without spring-boot). Someone decided to move some properties to external file. Unfortunately, the path is hardcoded in xml context file, so it looks like this:
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:hardcoded/path/props.properties</value>
</list>
</property>
</bean>
Both xml context file and property file are inside the archive. The application works fine, however, I need to change (override) some properties from the hardcoded file. Unfortunately, I can't modify or compile new jar. I tried adding files with overriden properties to the classpath (with the same name) and also passing some properties as jvm args, but it didn't work. How can I replace/override this file from classpath without modifying the original java archive?
I would take a look at the PropertyOverrideConfigurer.
Property resource configurer that overrides bean property values in an
application context definition. It pushes values from a properties
file into bean definitions.
That should allow you to "push" your own values on top of the prior-loaded set.
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'm using my context.xml file to set init parameters for my java application, for example:
<Parameter
name="Environment"
description="The environment in which this code is running (e.g. Production, Staging, Development)."
value="Production"/>
I would like to be able to create a parameter who's value attribute is loading from a file. Is there anyway to do this? Should I be using a < Resource > element instead? If so, how do I setup a resource to load the contents of a file? I've tried Google, but I my not understand the context.xml file well enough to know what to look for. Any help is much appreciated!
In application-context.xml add this element
<context:property-placeholder ignore-unresolvable="true" location="classpath*:application.properties"/>
In the file application.properties you can define parameters like this
userName=root
password=123
And then you can use the parameters by this way
<property name="username" value="${userName}"/>
<property name="password" value="${password}"/>
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