Now I'm trying to load properties file in computer file system in my desktop application using spring framework. configuration folder have two files: datasource-tx-jpa.xml and database.properties (both files locate at same folder). But when I run this application then a message box appear with message can not load ${sqlserver.jdbc.driverClassName} in dataSource. Below is configuration of datasource-tx-jpa.xml.
<context:property-placeholder location="file:database.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${sqlserver.jdbc.driverClassName}" />
<property name="url" value="${sqlserver.jdbc.url}" />
<property name="username" value="${sqlserver.jdbc.username}" />
<property name="password" value="${sqlserver.jdbc.password}" />
</bean>
Since the file is on your resources folder, you can use <context:property-placeholder location="classpath*:database.properties" />
Related
I am trying to create a Spring boot application where I am using Amazon RDS mysql database and S3 for storing customer image.
But as I am running my main class: I am always getting
Bean of type 'com.amazonaws.services.s3.AmazonS3Client' that could not be found
Below is my project hierarchy:
My question is the spring configuration file is in the right folder?
As I find instantiate dataSource via spring xml more easy. And on the internet I haven't found single example where Spring boot Application has used Spring xml file? All are using annotations?
<bean id="customerDAO" class="com.sap.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://rdssample.xxxxxxp.us-west-2.rds.amazonaws.com:3306/customer" />
<property name="username" value="rdssample" />
<property name="password" value="rdssample" />
</bean>
Is my spring config file at the correct path?
Please help me with the solution.
I have 2 maven modules - Webapp and Persistence. Persistence module has a property file - database.properties and two context files - persistenceContext.xml (main), datasourceContext.xml (imported to persistenceContext.xml).
persistenceContext.xml
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.example.persistence"/>
<context:property-placeholder location="classpath:database.properties, classpath:application.properties" />
<import resource="classpath:datasourceContext.xml" />
Files persistenceContext.xml, datasourceContext.xml and database.properties are in directory resources of Persistence module.
datasourceContext.xml
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="${database.url}"/>
<property name="user" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="debugUnreturnedConnectionStackTraces" value="true"/>
<property name="unreturnedConnectionTimeout" value="20"/>
<property name="minPoolSize" value="5"/>
<property name="initialPoolSize" value="10"/>
<property name="maxPoolSize" value="50"/>
<property name="maxStatements" value="50"/>
<property name="idleConnectionTestPeriod" value="120"/>
<property name="maxIdleTime" value="1200"/>
</bean>
database.properties
database.url=jdbc:mysql://255.255.255.255:3306/...
database.username=...
database.password=...
Persistence maven module is dependency of Webapp module. When I deploy Webapp to Tomcat it failed with exception:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [spring-datasource.xml]: Could not resolve placeholder 'database.url' in string value "${database.url}"
I tried the following solutions:
moved database.properties under directory path resources\com\example\persistence.
use property-placeholder or PropertyPlaceholderConfigurer in datasourceContext.xml instead of using property-placeholder in persistenceContext.xml.
moved database.properties under Webapp module and then try point 1.
I always convinced that database.properties file is under classpath of Persistence module.
Nothing from mentioned doesn't work, I looked also on other threads on stackoverflow and spring forum - but with no success.
in my applicationcontext.xml i have this :
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dashboardsupervisor" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
here i am connecting with my database :
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
MySQLRdbHelper rdbHelper = (MySQLRdbHelper)
ctx.getBean("ManagerSupervisor");
What is want is to NOT read the password "1234" from my applicationcontext.xml
and read it from some properties file in my local drive .
as this will be running on different machines and every one have different passwords.
Can i achieve this .
thanks
Yes, you can, and the key to this is Springs PropertyPlaceholderConfigurer.
For example, you create a file on your file system called database.properties, containing your password (note, that you can also add more settings to this file, like username, JDBC url, etc).
jdbc.password=1234
Next, you need to declare a PropertyPlaceholderConfigurer bean and point it to the database.properties file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>path/to/database.properties</value>
</property>
</bean>
Note that the path is interpreted as a classpath resource, unless it is prefixed with file:.
Finally, replace the configuration of your dataSource bean: replace
<property name="password" value="1234" />
with
<property name="password" value="${jdbc.password}" />
You could use profiles:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev1");
ctx.load("*Context.xml");
ctx.refresh();
<bean id="database1" profile="dev1"/>
<bean id="database2" profile="dev2">
The best way is to create the data source in application server and configure as below in application.xml
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>YourDSName</value></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
.....
</bean>
I have a spring 3.1 application loading settings using PropertySourcesPlaceholderConfigurer and I want to manage test and production environments simply loading settings from server context overriding settings specified in local file properties.
Next example works fine with Tomcat, how can I do the same in JBoss AS 7.1?
In spring context I have:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="localOverride" value="false" />
<property name="locations">
<list>
<value>classpath:Application.properties</value>
<value>classpath:ApplicationTest.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
In ApplicationTest.properties (that overrides Application.properties):
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:#XXXX:1521:xxxx
jdbc.username=myUsername
jdbc.password=myPassword
In context.xml (in Tomcat/conf directory):
<Context>
...
<Parameter name="jdbc.driverClassName" value="oracle.jdbc.OracleDriver" override="false"/>
<Parameter name="jdbc.url" value="jdbc:oracle:thin:#XXXX:1521:ProductionSID" override="false"/>
<Parameter name="jdbc.username" value="myProductionUsername" override="false"/>
<Parameter name="jdbc.password" value="myProductionPassword" override="false"/>
...
</Context>
In this way all parameters specified in context.xml overrides parameters in ApplicationTest.properties.
Is there a way to specify context parameter in JBoss? (i.e. in standalone.xml file)
EDIT - SOLVED:
As suggested in the answers, if I put entries in WEB-INF/jboss-web.xml inside webapp that works:
<jboss-web>
...
<env-entry>
<env-entry-name>jdbc.username</env-entry-name>
<env-entry-value>myUsername</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
...
</jboss-web>
But my goal was to put the config file out of webapp.
I solved in this way, I load the config file from $SERVER/conf directory:
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="localOverride" value="false" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:Application.properties</value>
<value>classpath:ApplicationTEST.properties</value>
<value>file:${catalina.home}\conf\ApplicationPRODUCTION.properties</value><!-- FOR TOMCAT -->
<value>file:${jboss.server.base.dir}\configuration\ApplicationPRODUCTION.properties</value><!-- FOR JBOSS-->
</list>
</property>
</bean>
If someone knows how to put Parameters in JBOSS config out of webapp (as TOMCAT) is well appreciated!
Thanks All!
According to the JBoss7 documentation:
In AS7 the file context.xml is ignored. Most of the old context.xml
configuration has been moved to jboss-web.xml.
See here for more details.
In older versions you could add these properties to context.xml as usual - JBoss use Tomcat as a servlet container.
Use a jboss-web.xml file. This file specifically replaces context.xml from Tomcat.
I want to use jndi in my spring mvc application for 2 things:
datasource settings
store the location of my properties file so I can do a lookup in my code
I actually have 2 applications, 1 is a spring mvc and the other is a spring so I'm planning on using the same jndi settings for both.
Also, the 2 applications run on different contains (tomcat and jetty), I've never used jndi before so I hope I can define this in a single location and have both applications point to the same jndi file (assuming its a file).
So as for #1, my datasource settings in my spring context file are currently:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/mydb_development"/>
<property name="username" value="devuser1"/>
<property name="password" value="123"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="testOnBorrow" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
Can someone tell me how I can extract this out to use JNDI, confused, is it a separate file that has to be in my class loader, or do I hard code the path in my spring context file?
And as for #2, in my code I am currently loading a properties file that is in my class path like:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream("test.properties");
Now the test.properties file is in my classpath so it works out of the box, but what I want to know/understand is if I could somehow use jndi to lookup the location of the properties file (if that makes sense?) and not have this file in my classpath potentially.
I did google around and I know in spring I can pull in jndi using:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/mysqlDataSource" />
But what I don't understand is, where is the actual file that has the settings? Can it be in a location other than my web.xml file? (my other application again is using spring, but it is a daemon so it doesn't have a web.xml file).
The goal for me is to be able to change the values of the jndi file (like username, passwords, file paths) and NOT have these embedded into a jar. I want to be able to manually edit these files on the production server or dev servers and just restart the container.
I'm kinda rusty with spring but I remember using a PropertyPlaceHolderConfigurer, something like:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound">
<value>true</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
<property name="locations">
<list>
<value>file:path to your file
</value>
<value>file:path to another file if needed
</value>
</list>
</property>
</bean>
Then you can use the values on the .properties defined on the list directly. i.e. if you have a property like
db.driver=com.mysql.jdbc.Driver
you can use it like
bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
when you want to define a value.
If you want further control over the file, the path to the file itself can be registered on your server and you can look it up through JNDI, changing
file:path to your file
into
file:${propertiesFilePath}
I'm pretty sure you can look up those configuration properties by code too by a normal JNDI lookup.