I want to set my db file location as part of system variable which shall be provided at time of starting application.
I want to access this system property in applicationContext.xml for which I have tried to use #{systemProperties['db.properties']}in applicationContext.xml.
I have defined propertyPlaceholderConfigurer bean in 2 following ways specifying value for property "locations" in 2 different ways:
Case 1 #:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true"/>
<property name="locations">
<value>file://"#{systemProperties['db.properties']}"</value>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
dataSource bean #:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.pwd}"/>
</bean>
Case 2 #:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true"/>
<property name="locations">
<value>file://${DB_CONF}/test/db.properties</value>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
If I use the value of property locations as in case 2 instead of
"#{systemProperties['db.properties']}" application works perfectly OK.
In the case 1, I try to access the same properties file from same location as a System property but application does not work with error as follows:
Stack Trace
Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:235)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:450)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:450)
... 133 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL '${db.url}'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
... 136 more
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
Here you find how to read properties file using xml
<bean id="propertyBean"
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<!-- This will find you file inside system classpath -->
<value>classpath:application.properties</value>
<!-- or you pass the whole path -->
<!--<value>file:///opt/application.properties</value> -->
</list>
</property>
</bean>
I could resolve the error.The culprit was double quotes.
Using file://#{systemProperties['db.properties']} instead of file://"#{systemProperties['db.properties']}" resolved the issue.
Related
I am trying external configuration with properties file. dataSource bean is defined in security-context-xml file and hibernate.cfg.xml use them for database informations and sessionFactory.
I want externally dataSource configuration with encrypted password.
Used code following:
<context:property-placeholder
location="classpath:sample.properties" ignore-unresolvable="true" />
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.OracleDriver" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:hibernate.cfg.xml" />
</bean>
And sample.properties file is:
jdbc.url="*****"
jdbc.username="****"
jdbc.password="****"
This didn't work and throws exception:
Caused by: java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL '${jdbc.url}'
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2023)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1897)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
... 56 more
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>
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.
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.
hi i am going to develop web-services in java (NetBeans) but have problems with get conection, i get the following error:
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.net.UnknownHostException: C
Caused by: java.net.UnknownHostException: C
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at sun.net.ftp.impl.FtpClient.doConnect(FtpClient.java:957)
at sun.net.ftp.impl.FtpClient.tryConnect(FtpClient.java:917)
at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1012)
at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:998)
at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:294)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:393)
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:103)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:179)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:158)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(Proper at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:68)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:467)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:334)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:122)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:76)
at com.rsa.ims.components.spring.SimpleApplicationLoader.loadBeanFactory(SimpleApplicationLoader.java:134)
at com.rsa.ims.components.spring.SimpleApplicationLoader.useBeanFactory(SimpleApplicationLoader.java:105)
at com.rsa.command.ConnectionFactory.getSpringBeanTarget(ConnectionFactory.java:183)
at com.rsa.command.ConnectionFactory.getTarget(ConnectionFactory.java:156)
at com.rsa.command.ConnectionFactory.getConnection(ConnectionFactory.java:238)
at com.rsa.command.ConnectionFactory.getConnection(ConnectionFactory.java:281)
at com.arame.rsa.cmdConnection.getConnection(cmdConnection.java:33)
at com.arame.rsa.ApiRsaTest.main(ApiRsaTest.java:49)
Exception in thread "main" java.lang.NullPointerException
at com.arame.rsa.cmdConnection.closeConnection(cmdConnection.java:59)
at com.arame.rsa.ApiRsaTest.main(ApiRsaTest.java:94)
I have the config.properties where is the URL and Context.xml this file processing to retrieve properties from a file config.properties also have log4j.xml/dtd all them in my classpath.
this is my con Context.xml
<beans default-lazy-init="true" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<bean name="PrimaryPropertyPlaceHolderPostProcessor" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="10"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="location" value="file://C:\\Users\\gerardo.granados\\Documents\\APITest\\build\\classes\\config.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="ignoreResourceNotFound" value="false"/>
</bean>
<bean name="ConnectionInfoProvider"
class="com.rsa.command.PropertiesConnectionInfoProvider"
lazy-init="false">
<property name="propertiesFile" value="config.properties"/>
</bean>
<alias alias="CommandAPIConnection" name="SOAPCommandTargetBasicAuth"/>
<bean name="ClusterTarget" lazy-init="true"
class="com.rsa.command.RemoteCommandTargetFactoryBean">
<property name="providerURL" value="t3s://node1-address,node2-address:7002"/>
<property name="connectionInfoProvider" ref="ConnectionInfoProvider"/>
<property name="targetClass" value="com.rsa.command.EJBRemoteTarget"/>
<property name="cacheable" value="true"/>
</bean>
<bean name="SpecificNodeTarget" lazy-init="true"
class="com.rsa.command.RemoteCommandTargetFactoryBean">
<property name="providerURL" value="t3s://node2-address:7002"/>
<property name="connectionInfoProvider" ref="ConnectionInfoProvider"/>
<property name="targetClass" value="com.rsa.command.EJBRemoteTarget"/>
<property name="cacheable" value="true"/>
<property name="properties">
<props>
<prop key="com.rsa.naming.pin.to.primary.server">true</prop>
</props>
</property>
</bean>
</beans>
Any suggestion???? Thanks
In the line:
<property name="location" value="file://C:\\Users\\gerardo.granados\\Documents\\APITest\\build\\classes\\config.properties"/>
Remove the first two slashes after file:. Like this:
<property name="location" value="file:C:\\Users\\gerardo.granados\\Documents\\APITest\\build\\classes\\config.properties"/>
This worked for me in a related issue.