How do I instantiate AmazonS3Client via Spring? - java

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.

Related

Configure SessionFactory of JPA into Spring

I am trying to add a Jar to my project that uses Spring 4 sessionFactory to connect to DB. My Project uses Spring boot with JPA for transactions.
How do I configure my project in order that the JPA sessionFactory is passed to the Spring sessionFactory? .
When I put the jar in my project, I got the error: "Consider defining a bean named 'sessionFactory' in your configuration."
So, I add a sessionFactory on my applicationContext.xml:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${spring.datasource.driver-class-name}" />
<property name="url" value="${spring.datasource.url}" />
<property name="username" value="${spring.datasource.username}" />
<property name="password" value="${spring.datasource.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.secondProjectClass</value>
</list>
</property>
</bean>
But, when I add this, the following error appears:
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
My projects has spring-boot-starter-data-jpa:2.2.4.RELEASE, and the jar uses Spring 4.
Currently I am not able to edit the second jar (the one with spring 4).
Is this configuration possible? Thanks!
First project config: (Base project)
implementation ('org.springframework.boot:spring-boot-starter-data-jdbc')
implementation ('org.springframework.boot:spring-boot-starter-web')
compile ('org.springframework.boot:spring-boot-starter-data-jpa:2.2.4.RELEASE')
Second project config: (Jar that I need to add)
springVersion = '4.3.13.RELEASE'
compile("org.springframework:spring-context:${springVersion}")
compile("org.springframework:spring-web:${springVersion}")
compile("org.springframework:spring-tx:${springVersion}")
I solved this by keeping only SpringBoot + JPA Repositories, everything in the same version.

Spring Beans: DriverManagerDataSource converting to sun.jdbc.odbc.ee.DataSource

I have a Spring MVC project that I am using NamedParameterJdbcTemplate to connect to a postgres database.
When I try to launch my application, I get an exception:
Cannot convert value of type
[org.springframework.jdbc.datasource.DriverManagerDataSource]
to required type [sun.jdbc.odbc.ee.DataSource] for
property 'dataSource': no matching editors or conversion strategy found
My dataSource bean looks like this:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/********" />
<property name="username" value="postgres" />
<property name="password" value="********" />
</bean>
I have connected to postgres with NamedParameterJdbcTemplate before. Am I missing a dependency that makes this conversion possible, or what is the problem?
As the exception itsel reveals that it is expecting sun.jdbc.odbc.ee.DataSource
and found org.springframework.jdbc.datasource.DriverManagerDataSource
That implies you have wrong import wherever you are using DataSource class make sure it is imported from javax.sql.DataSource

openshift mysql connection NumberFormatException with Tomcat 7 (JBoss EWS 2.0)

I am new to openshift. I created a sample application with spring security and tried to deploy in it. My spring-database.xml fragment looks like below.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://${env.OPENSHIFT_MYSQL_DB_HOST}:${env.OPENSHIFT_MYSQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}" />
<property name="username" value="${env.OPENSHIFT_MYSQL_DB_USERNAME}" />
<property name="password" value="${env.OPENSHIFT_MYSQL_DB_PASSWORD}" />
</bean>
and I am getting following exception in the cloud environment.
Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "${env.OPENSHIFT_MYSQL_DB_PORT}"'
The following are my catrige/gear configuration
Mysql 5.1
Tomcat 7 (JBoss EWS 2.0)
any solutions how to overcome this issue?
The problem is the usage of expression like ${env.OPENSHIFT_MYSQL_DB_HOST}.
Those are operating system environment variables and as you can see in the exception that instead of trying to read the values of those system environment variables Spring is trying to turn it into a number.
Try something like #{systemEnvironment[OPENSHIFT_MYSQL_DB_HOST]} to read the OS environment variables. That's an expression to read system environment variable called OPENSHIFT_MYSQL_DB_HOST.
For whom like me that the accpeted asnwer did not work for them I used this answer
I was just putting
<bean id="propertyPlaceholderChttps://stackoverflow.com/a/3965727/1460591onfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></bean>
into application context with adding varibales like this
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}" />
<property name="username" value="${OPENSHIFT_MYSQL_DB_USERNAME}" />
<property name="password" value="${OPENSHIFT_MYSQL_DB_PASSWORD}" />
</bean>
And It all worked just fine

How to load properties file using spring framework in file system

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" />

Embedded Derby Db in a Spring app on Tomcat

I'm trying to get an embedded Derby db running on a Tomcat/Spring application.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="url" value="jdbc:derby:pepper" />
<property name="initialSize" value="5" />
<property name="maxActive" value="50" />
</bean>
When I run this, I'm getting the following error:
org.apache.commons.dbcp.SQLNestedException:
Cannot create
PoolableConnectionFactory (Database
'WEB-INF/pepper' not found.)
I've tried the pepper folder at both %webapp_root%/pepper and %webapp_root%/WEB-INF/pepper
Suggestions?
If you're deploying a web app to Tomcat, I'd recommend setting up a JNDI connection pool and using Spring's JndiObjectFactoryBean:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/blah"/>
</bean>
I guess you need to replace url with jdbc:derby:pepper;create=true

Categories

Resources