how to use values from external property files in spring xml configuration? - java

I tryed to pull password from hibernate config to separate properies file.
Initially I had this config:
...
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
...
<property name="username" value=root />
...
After I wrote following
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
...
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
...
<property name="username" value="${db.username}" />
...
in db.properties I wrote following:
username=root
I got following trace:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [db.properties] cannot be opened because it does not exist
file with configurationa and db.properties locates in same folder.

You can ignore the classpath: keyword and just use db.properties in the list.

it working after:
put db.properties to src/main/resources
replace this configuration
${db.username}
with
${username}

Related

org.springframework.beans.NotWritablePropertyException. Invalid property <blah> is not writable or has an invalid setter method

I have the requirement to remove all passwords and encryption keys from the source code of my project. I'm struggling to get this to work in my spring-servlet.xml file.
This worked before I tried making the change:
the database username, encrypted password, URL and driver were defined in a file jdbc_server.properties as a classpath resource.
the encryption/decryption key was passed on start-up as -DENCRYPTION_PASSWORD=
I want to move the jdbc_server_properties file to the filesystem and include the key in the file.
This is my last (of at least 25) attempt at getting this to work.
<bean id="propertyConfigurer" class="org.jasypt.spring4.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="standardEncryptor" />
<property name="jdbcUsername" value="JDBC_USERNAME" />
<property name="jdbcPassword" value="JDBC_PASSWORD" />
<property name="jdbcUrl" value="JDBC_URL" />
<property name="jdbcDriver" value="JDBC_DRIVER" />
<property name="locations">
<list>
<value>file:///${USERPROFILE}/credentials/jdbc_server.properties</value>
</list>
</property>
</bean>
<bean id="standardEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="environmentVariablesConfiguration" />
</bean>
<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="passwordSysPropertyName" value="ENCRYPTION_PASSWORD" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbcDriver}" p:url="${jdbcUrl}"
p:username="${jdbcUsername}" p:password="${jdbcPassword}">
</bean>
Using the above configuration, I get a NotWritablePropertyException exception. I've seen tons of posts on this issue but not where both the properties and encryption key are in a file on the filesystem.
When this was working and the properties were read from a file in the classpath, there were no getters/setters for jdbcUsername (or the other properties) so I don't know why it's failing in this way now.
I tried adding the getters and setters (as String) to my BaseDaoImpl class but I still get the same error so if I'm supposed to add them, I'm not sure where they go.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertyConfigurer' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcUsername' of bean class [org.jasypt.spring4.properties.EncryptablePropertyPlaceholderConfigurer]: Bean property 'jdbcUsername' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1568)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
at ...
That is probably because jdbcUsername is not a property of EncryptablePropertyPlaceholderConfigurer.
Please, try something like this (just remove the jdbc* properties from the EncryptablePropertyPlaceholderConfigurer configuration, and use it directly in your dataSource bean):
<bean id="propertyConfigurer" class="org.jasypt.spring4.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="standardEncryptor" />
<property name="locations">
<list>
<value>file:///${USERPROFILE}/credentials/jdbc_server.properties</value>
</list>
</property>
</bean>
<bean id="standardEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config" ref="environmentVariablesConfiguration" />
</bean>
<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="passwordSysPropertyName" value="ENCRYPTION_PASSWORD" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${JDBC_DRIVER}" p:url="${JDBC_URL}"
p:username="${JDBC_USERNAME}" p:password="${JDBC_PASSWORD}">
</bean>
Assuming your jdbc_server.properties file contains the required information:
JDBC_USERNAME=ENC(...)
JDBC_PASSWORD=ENC(...)
JDBC_URL=ENC(...)
JDBC_DRIVER=your.jdbc.driver

How I can configure properties file right?

I have maven project with several modules
this is part of database-config.xml in office-core module:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
I have application.properties files on several places:
c:\projects\office\app\office-core\application.properties
c:\projects\office\app\office-core\src\main\profiles\application.properties
c:\projects\office\app\application.properties
In my opinion with this config in database-config.xml I should can use the properties file in my database-config.xml
but I cant... Any help ?
You should put the application.properties file to src/main/resources directory to have it effectively on classpath. Then it should work.
I think it will not work because those paths are not part of your classpath. I suggest you to have some default configuration and to have configuration override per module, something like that:
<bean id="overrideConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="location" value="file:/SomeFolderInFileSystem/conf.properties"/>
<property name="ignoreResourceNotFound" value="true"/>
</bean>

Spring - Set properties before load resources in applicationContext

in my applicationContext.xml I have a list of spring cfg files.
<import resource="classpath:resource1.xml"/>
<import resource="classpath:META-INF/resource1.xml"/>
<import resource="classpath:META-INF/resource1.xml"/>
<import resource="classpath:resource1.xml"/>
<import resource="classpath:resource1.xml"/>
And these files need some properties that are setted.
When I'm set these properties in JVM every runs OK.
But when load by properties file in my applicationContext, I receive an exception because the app couldn't "translate" the property key(${key}) in value.
<bean id="propSource" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="-999"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="location">
<value>classpath:/application.properties</value>
</property>
</bean>
I think that spring load import xml files before to load properties file.
How I can solve this?
Thnks

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>

Spring 3.0 Could not load properties

Im the getting the following Error/exception when deploying web app:
org.springframework.beans.factory.BeanInitializationException: Could
not load properties; nested exception is
java.io.FileNotFoundException: Could not open ServletContext resource
[/WEB-INF/WebAppProps]
Below is the context tag that Im using in applicationContext.xml to point to WebAppsProps.properties file
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:/WEB-INF/WebAppProps.properties" />
I have also used:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/WebAppProps.properties" />
The file is actualy in the filesystem & below is the snapshot of my project structure:
I also tried , putting "WebAppProps.properties" on the classpath and used these 2 variations
variation 1:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:WebAppProps.properties</value>
</property>
</bean>
variation 2:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WebAppProps.properties</value>
</property>
</bean>
Please see below:
However Im still getting same error/exception.
Please Advise
Thank you
Same issue i crossed,
Fix:
Solution
your bean like below:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/database.properties</value>
</property>
</bean>
create folder properties under WEB-INF it look like WEB-INF/properties/database.properties
because the class PropertyPlaceholderConfigurer default search properties folder first under /WEB-INF/ so it concatenate your file name as path
I think that you should change your code and put the properties like that :
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties" />
</bean>
Spring 4 this is how I would configure the properties file
#Configuration
#PropertySources({
#PropertySource("classpath:application.properties"),
#PropertySource(value = "${application.properties}", ignoreResourceNotFound = false)
})
public class WebServiceConfig {
---
}
And when starting my tomcat in setenv.sh I define the path as
-Dapplication.properties=file:location-of-file/application.properties"
Notice file: prefix here. That is important.
Now you can drop file anywhere on your file system and change the path without ever changing the code
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WebAppProps.properties</value>
</property>
</bean>
Just put the file on the classpath in the root of the classes directory.
you have 2 options:
if you dont need to get the bean, but only the properties:
<context:property-placeholder location="/WEB-INF/WebAppProps.properties" file-encoding="UTF-8"/>
or if you need the bean (it may happens you want to inject your properties bean, if you need to read many props...)
<bean id="configProperty" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/config.properties</value>
<value>/WEB-INF/setup.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>

Categories

Resources