Alfresco : Access to properties file from java class - java

I want to know how to access to a properties file that is under share from a java class (under alfresco) which will be exported later as a .jar file under alfresco/lib.

As reported in this Alfresco forum post you should register a Spring bean in your context exploiting the PropertyPlaceholderConfigurer, setting the proper location of you properties file will be found.
<bean id="custom-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
<property name="locations">
<list>
<value>classpath:alfresco/extension/custom.properties</value>
</list>
</property>
</bean>
From now on every time you define a bean you can use placeholders taken from the property file, which has a common key=value format.

Related

PropertyPlaceholderConfigurer : Unable to read multiple property files

Below is the part of spring-context.xml where PropertyPlaceholderConfigurer bean is defined:
<bean id="propertyResource" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:property-files/config-info.properties</value>
<value>classpath:property-files/biservices-config.properties</value>
<value>classpath:dao-config.properties</value>
<value>classpath:environmentLocator.properties</value>
</list>
</property>
</bean>
However, after putting a breakpoint # PropertyPlaceholderConfigurer#setLocations I didn't find all the property files being passed to the setter method.
Please note that the file environmentLocator.properties and dao-config.properties are present in different jar files, although they are on the classpath.
What could be the issue?

how can i access properties file filed using applicationcontext.xml file in spring?

i added following in my applicationContext File
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:stage.properties</value>
<value>classpath:environment.properties</value>
</list>
</property>
</bean>
where i need to access stage.properties file values .stage.properties file is in src/main/resources
i have written following line in my java class to access this file
#Value("${spring.username}")
private String usr;
but i am getting valu for usr is like =${spring.username}
what i am missing here?
#Value("${spring.username}") notation requires the use of PropertyPalceholderConfigurer / PropertySourcesPlaceholderConfigurer depending on Spring version to resolve property placeholder. Either
Solution 1
Replace
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:stage.properties</value>
<value>classpath:environment.properties</value>
</list>
</property>
</bean>
with
<context:property-placeholder location="classpath:stage.propertes,classpath:environment.properties"/>
Make sure to import the Spring context namespace
Solution 2.
Use SpEL to access your properties bean with #Value as follows
#Value("#{properties['spring.username']}
private String usr;
This will access the spring.username property of the properties bean in your context

override property value ${abc.xyz} in bean property in spring configuration file

I have beans xml file, which loads multiple property files for creating its beans. All these property files are under a root folder like root/abc/abc.properties , root/xyz/some.properties etc..
<bean id="x".....
....
<util:properties id="properties" location="${config.base.dir}/abc/abc.properties" />
......
</bean>
<bean id="y".....
....
<util:properties id="properties" location="${config.base.dir}/xyz/some.properties" />
......
</bean>
I want to override put the value of config.base.dir somewhere in top so that I can keep changing the root location, should this be possible by defining some property on top?
If using Maven, you can have a version of abc.properties in the test/resources/abc/ folder. this will be picked up on the classpath before the main/resources/abc/abc.properties file.
Does this help?
Why do you want to ' keep changing the root location' ?
system properties overrides...
<!-- Configuration property files -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
<property name="locations">
<list>
<value>classpath*:config.properties</value>
</list>
</property>
</bean>

How can i locate files in spring that arent in the same level as the spring.xml?

Let's suppose i have a message source bean like this:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property>
<list>
<value>mymessages</value>
<value>mymessages_en_US</value>
</list>
</property>
</bean>
This will only work if my .properties are in the same level as the spring.xml
how would i locate the files if my .properties where in a folder properties
inside the project?
ResourceBundleMessasgeSource loads resource bundles from the classpath.
If you ensure that the folder where the properties files reside is on the classpath then Spring should load them.

How to set dynamically a bean reference in Spring?

<bean id="Mybean" class="Bean">
<property name="config" ref="dev"/>
</bean>
<bean id="dev" class="Dev">
<property name="x" ref="Dev1">
<property name="y" ref="Dev2">
<property name="z" ref="Dev3">
</bean>
<bean id="stag" class="Dev">
<property name="x" ref="Stag1">
<property name="y" ref="Stag2">
<property name="z" ref="Stag3">
</bean>
In the above scenario, the config property in the bean MyBean change from environment to environment. At the time of dev, reference of config change to dev. And in staging, the reference change to stag. The problem comes at the time of checked in the spring file. We have to analyze everytime the reference of config before checked in. If the reference of config with the value of dev checked in, we might have to explain a lot of questions.
Is there any solution to solve to make it automate?
Note: Spring version is 2.0.1
Use the PropertyPlaceholderConfigurer from Spring, and remove an unused bean :
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>env.properties</value>
</property>
</bean>
<bean id="Mybean" class="Bean">
<property name="config" ref="config"/>
</bean>
<bean id="config" class="Config">
<property name="x" ref="${x}">
<property name="y" ref="${y}">
<property name="z" ref="${z}">
</bean>
and the env.properties file contains the following properties :
x=Dev1
y=Dev2
z=Dev3
or
x=Stag1
y=Stag2
z=Stag3
setup up the placeholder bean by specfiy, let spring know you want the placeholder
set up the config for the "my bean" by using the "${env}"
for example:
<beans>
<bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>env.properties</value></property>
</bean>
<bean id="Mybean" class="Bean">
<property name="config" ref="${env}"/>
</bean>
</beans>
and you need the add the env = dev key-value to the env.properties file
Assuming you meant Spring 3.1, rather than Spring 2.1 (which doesn't exist), then you can use the new "Environment Profiles" feature that was introduced in 3.1. This allows you to define a set of beans for each of your environments, and then select the "active" one at runtime.
See this SpringSource Blog Entry for examples.
You can do it using PropertyPlaceholderConfigurer or using #Profile
Also See
is-there-any-way-to-enable-or-disable-the-spring-bean-definition-in-applicationc
PropertyPlaceholderConfigurer example
PropertyPlaceholderConfigurer is the answer, yet I would imagine that you would like this to happen without the need to stay updating your properties file for each environment.
My suggestion would therefore be as follows
Use PropertyPlaceholderConfigurer, but do not create a properties file
By default, PropertyPlaceholderConfigurer first tries to find a value in a properties file, but if that fails, it will look for one in system properties
So all you need to do is to define both beans the same way that you are doing it, i.e. dev and stag.. which is a fine approach since you're clearly showing the different configurations... it would help if you also added some alias to show clearly the setting you want to use.
Next, pass in a system property defining what mode you are in... and ideally explicitly set PropertyPlaceholderConfigurer to use System properties.
So.. your config would look something like this
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
systemPropertiesMode="2"/>
<bean id="Mybean" class="Bean">
<property name="config" ref="${launch.mode}"/>
</bean>
<bean id="dev" name="dev_mode" class="Dev">
<property name="x" ref="Dev1">
<property name="y" ref="Dev2">
<property name="z" ref="Dev3">
</bean>
<bean id="stag" name="staging_mode" class="Dev">
<property name="x" ref="Stag1">
<property name="y" ref="Stag2">
<property name="z" ref="Stag3">
</bean>
You can then pass in the property upon startup in the following fashion
-D<property-name>=<value>
So in this case you'd use
-Dlaunch.mode=dev_mode
Or
-Dlaunch.mode=staging_mode
And you won't need to touch any of the configuration files.
Just a further note on systemPropertiesMode, accepted values are the following:
0 - never look in system properties
1 - use system properties as a fallback (i.e. if not found in properties files)
2 - system properties always override (the mode i'm suggesting)
Hope it helps :)
Note: This recommendation is only applicable to Spring < 3.1, since from 3.1 onward, the recommended approach is to use #Profile
Spring provides a mechanism called property placeholders. This way you can set certain properties in a database/properties file and spring will fill them in on startup.
The class to use for this is located here.

Categories

Resources