Loading multiple external properties files from spring - java

I want to load all the properties files with Spring from an external folder. I successfully load one file but adding a wildcard to the mix does not seem to work.
This works (load test.properties):
<bean id="propertiesLocation"
class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
<property name="initParamName"><value>file://EXTERNAL_DIRECTORY/test.properties</value></property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" ref="propertiesLocation"></property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="0"/>
</bean>
This does not (load *.properties):
<bean id="propertiesLocation"
class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
<property name="initParamName"><value>file://EXTERNAL_DIRECTORY/*.properties</value></property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" ref="propertiesLocation"></property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="0"/>
</bean>
The error:
Caused by: java.io.FileNotFoundException: /EXTERNAL_DIRECTORY/*.properties (No es un directorio)
How can I make Spring load all the external properties files in a folder?
Edit: I use the first bean (ServletContextParameterFactoryBean) because in the project I retrieve the path from the web.xml file. I forgot about this and just pasted the path in the bean, it is incorrect but has nothing to do with the question.

Try use following:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="file://EXTERNAL_DIRECTORY/*.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="0"/>
</bean>
If you need include more resources you can do next:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" >
<list>
<value>classpath:single.properties"</value>
<value>file://EXTERNAL_DIRECTORY/*.properties"</value>
<value>file://ANOTHER_EXTERNAL_DIRECTORY/*.properties"</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="0"/>
</bean>
With default implementation of PropertyEditor, Spring will convert strings into Resource. You can find details in documentation.
Hope this will be helpful.

Related

Hibernate trying to find hibernate.cfg.xml when configured by spring

I have configured Hibernate(5.2.6) via spring(4.3.5) But when I run my Junit(4.12) tests it gives out an error saying that it could not locate hibernate.cfg.xml file. Can anyone help me out?
After I run the Junit test, tables are created in database but data is not added, I can see the sql queries being run as I have enables show_sql
did you specify hibernate configured xml file name in web.xml.?
if not specify name of xml file in web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hibernateContext.xml</param-value>
</context-param>
How is applicationContext is configured for spring and hibernate integration?
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:properties/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>

Change the default datasource in spring batch

I want to use mysql database in spring batch application. Where should I keep my XML file with the details of datasource.
currently i have kept the env-context.xml in src/main/resources/META-INF/batch/override/manager
Content of env-context.xml:
<!-- connect to database -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/demo_db" />
<property name="username" value="testuser" />
<property name="password" value="12345" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
The common approach is to put this kind of configuration parameters into a properties file in order to externalize them as they will change from one environment to another.
It can be done as next:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:datasource.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${datasource.driver}" />
<property name="url" value="${datasource.url}" />
<property name="username" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
Then you put your file datasource.properties into the classpath, this file will have a content of type:
datasource.driver=com.mysql.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/demo_db
datasource.username=testuser
datasource.password=12345

ActiveMQ Data persistent Issue in Java Spring

I have an application which uses embedded activeMQ 5.11. At the start of the application it creates activemq-data\producerBroker\KahaDB folder at the class path location. I do want to change the location but spring.xml doesn't take a location.
Spring.xml as given,
<bean id="producerBroker" class="org.apache.activemq.broker.SslBrokerService">
<property name="brokerName" value="producerBroker" />
<property name="persistent" value="true" />
<property name="persistenceAdapter" ref="persistenceAdapter"/>
<property name="transportConnectors">
<list>
<bean class="org.apache.activemq.broker.TransportConnector">
<property name="name" value="xxx"></property>
<property name="uri" value="${transportConnectorURIs}"></property>
</bean>
</list>
</property>
<property name="jmsBridgeConnectors">
<list>
<bean class="org.apache.activemq.network.jms.JmsQueueConnector">
<property name="outboundQueueConnectionFactory">
<bean class="org.apache.activemq.ActiveMQSslConnectionFactory">
<property name="brokerURL" value="${brokerURL}" />
<property name="userName" value="${username}" />
<property name="password" value="${password}" />
<property name="trustStore" value="${trust.store.path}" />
<property name="trustStorePassword" value="${trust.store.password}" />
<!-- <property name="keyStore" value="${key.store.path}"/> -->
<!-- <property name="keyStorePassword" value="${key.store.password}"/> -->
</bean>
</property>
<property name="outboundQueueBridges">
<list>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${screenshotQueueName}" />
</bean>
<bean class="org.apache.activemq.network.jms.OutboundQueueBridge">
<constructor-arg value="${resultXmlQueueName}" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="persistenceAdapter" class="org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter">
<property name="directory" value="E:\test"/>
Current issue is it throws an error as "exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.activemq.store.kahadaptor.KahaPersistenceA
apter] for bean with name 'kahaPersistenceAdapter' defined in class path resource [spring/resultupload/resultupload.xml]; nested exception is java.la
g.ClassNotFoundException: org.apache.activemq.store.kahadaptor.KahaPersistenceAdapter"
Anyone has experience in the directory change in activeMQ 5.11 in java spring?
The destination of the persistence location must be defined at the broker level.
The kahaPersistenceAdapter (which was file based) was removed with version 5.9. You should use the kahaDB.
kahaDB - uses KahaDB an embedded lightweight non-relational database
<broker brokerName="broker" persistent="true" useShutdownHook="false">
<transportConnectors>
<transportConnector uri="tcp://localhost:61616"/>
</transportConnectors>
<persistenceAdapter>
<kahaDB directory="e:/temp" ... />
</persistenceAdapter>
</broker>
all valid attributes: http://activemq.apache.org/schema/core/activemq-core-5.11.0-schema.html#kahaDB

Reading Dual/Multiple Properties in Spring

I'm trying to read the application specific properties from database, and I'm trying to keep the database credentials in properties file.
So I need to load both properties (File and DB), while the application is
loading.
I'm having my configuration as follows.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/config/db.properties"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="properties">
<bean class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
<constructor-arg>
<bean class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="postgresqlDataSource"/>
<constructor-arg value="TBL_APP_SETTINGS"/>
<constructor-arg value="PROP_KEY"/>
<constructor-arg value="PROP_VALUE"/>
</bean>
</constructor-arg>
</bean>
</property>
</bean>
<bean id="postgresqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${db.url}"/>
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.pass}"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="5"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="timeBetweenEvictionRunsMillis" value="5000"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="minEvictableIdleTimeMillis" value="30000"/>
<property name="minIdle" value="3"/>
</bean>
It's throwing error for ${db.url}
please help how to do this? thanks in advance.
The easiest way is to externalize some of your config properties the guide is on spring.io site. You can pass properies as java system properties or command line arguments
The second way is to write your own PropertyPlaceholderConfigurer which will read properties from application.yml and from the database
I believe the easiest way you can do it is writing credentials into the spring xml and import it to your main xml and reference them as beans:
<bean id="dbUrl" class="java.lang.String">
<constructor-arg type="java.lang.String" value="jdbc://..."/>
</bean>
... same for dbUser and dbPassword
And then refer to the value using ref.

ReloadingPropertyPlaceholderConfigurer

For some reason the ReloadablePropertiesFactoryBean doens't seem to do what I expect. When I change a property in the test.properties file I get a log message saying:
9979 [timer] INFO com.krest.core.properties.ReloadablePropertiesFactoryBean - Reloading Properties...
But for some reason nothing happens. The old property value is still set on my test bean. Does anybody know why this is? I use Spring 3 and my configuration looks like this:
<bean id="configproperties"
class="com.krest.core.properties.ReloadablePropertiesFactoryBean">
<property name="location"
value="classpath:test.properties" />
</bean>
<bean id="propertyConfigurer"
class="com.krest.core.properties.ReloadingPropertyPlaceholderConfigurer">
<property name="properties" ref="configproperties" />
<property name="reloadingPlaceholderPrefix" value="rel{" />
<property name="reloadingPlaceholderSuffix" value="}" />
</bean>
<bean id="mybean" class="nl.mycompany.TestBean">
<property name="message" value="rel{timer-delay}" />
</bean>
<!-- regularly reload property files. -->
<bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<bean id="reloadProperties"
class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="period" value="1000" />
<property name="runnable">
<bean class="com.krest.core.properties.ReloadConfiguration">
<property name="reconfigurableBeans">
<list>
<ref bean="configproperties" />
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>

Categories