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.
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 Spring configuration below. How I can make Spring content initialization even if DB_JNDI does not exist in Application server?
<jee:jndi-lookup id="friends.db" jndi-name="DB_JNDI"/>
<bean name="friendsDbDaoImpl" class="com.tims.db.friendsDbDaoImpl" init-method="init" >
<property name="dataSource" ref="friends.db" />
<property name="queryTimeout" value="${imos.db.friends.querytimeout}" />
</bean>
You can use Spring profiles. They were implemented with this kind of requirement (conditional configuration) in mind.
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
I have two modules:
A and B
A is dependent on B
Each have a Spring bean:
A has bean_a
B has bean_b
However:
bean_b depends on bean_a, meaning bean_a must load 1st
In addition bean_b is a method which returns a bean_b.
So my code looks something like this:
Bean B definition
#Bean
#DependsOn("bean_a")
public BeanBClass getBeanB() {// Do something}
Module A application context:
<context:annotation-config/>
<context:component-scan base-package="bean_a_package, bean_b_package"/>
<import resource="classpath*:module_b_context.xml"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations">
<list>
Some relevant property files
</list>
</property>
</bean>
<bean id="bean_a" class="bean_a_package.bean_a_class">
<property name="someProperty" value="${someProperty}"/>
</bean>
module_b_context.xml doesn't have anything relevant for the question and doesn't have a PropertyPlaceholderConfigurer.
And whenever I run my application I get the message:
Could not resolve placeholder 'someProperty' in string value
"${someProperty}
Can I fix this somehow? I'm guessing that the order of bean loading doesn't work as I expected but I can't figure out how to fix it.
Thanks in advance
We have multiple war files packaged in an ear file. Some of the wars are using pure JDBC and we want to use hibernate for new wars (modules). We are using spring 2.5.6, hibernate 3.0 and jboss 4.2 server. We have following configuration for transactionmanager.
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:MyPool" />
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="allowCustomIsolationLevels">
<value>true</value>
</property>
</bean>
It is working fine for us.
Now I want to configure hibernate transactionManger for new modules in a separate xml file.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:/hibernate.cfg.xml"/>
</bean>
Generally we configure hiberanate transaction mangager in following way.
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
But I would like to use same JtaTransactionManager reference defined for JDBC configuration. Please help me in configuring this.
You do not really need JTA if you are not using 2 phase commits. Just configure Hibernate trans mgr and it is capable of handling both hibernate and jdbc transactions. This link might also be helpful http://forum.springsource.org/showthread.php?t=69864