0

I have a working Wicket+Spring+JPA+Hibernate+MySQL application with following beans.xml and persistence.xml files respectively.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- Scans for @Repository, @Service and @Component -->
    <context:component-scan base-package="com.abc.app" />

    <!-- Enable @Transactional support -->
    <tx:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="eppm" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />              
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/eppm" /> 
            <property name="hibernate.connection.username" value="xxx" />
            <property name="hibernate.connection.password" value="xxx" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

At this point I would like to use JdbcTemplate in order to use raw sql. For jdbcTemplate injection, when I include following to my beans.xml

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="textDao" class="com.fhlabs.dao.TestDaoService">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

At deployment time, end up with following error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in class path resource [beans.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
... 

How do I specify dataSource details explicitly here when all db details are specified in persistence.xml Many thanks.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
sdia2000
  • 1
  • 1
  • 1

1 Answers1

1

You are referencing a bean called dataSource, so you have to define it ... here is an example, first I load a config doc file :

<context:property-placeholder location="classpath:spring.properties"/>

Then declare the DataSource, which references the config files settings :

<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"
      p:driverClass="${app.jdbc.driverClassName}"
      p:jdbcUrl="${app.jdbc.url}"
      p:user="${app.jdbc.username}"
      p:password="${app.jdbc.password}"
      p:acquireIncrement="5"
      p:idleConnectionTestPeriod="60"
      p:maxPoolSize="100"
      p:maxStatements="50"
      p:minPoolSize="10"/>
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • what would be class that I need to specify when declare dataSource – sdia2000 Sep 14 '12 at 10:31
  • what class are you talking about ? driver class, it depends what database you are using, just use same as persistence.xml. Oh Anything that implements datasource interface, up to you ... – NimChimpsky Sep 14 '12 at 10:32
  • another option is org.apache.commons.dbcp.BasicDataSource , http://stackoverflow.com/questions/4190062/configure-spring-datasource-for-hibernate-and-transactional – NimChimpsky Sep 14 '12 at 10:36
  • do you mean something like will work. sorry for this, am just lost! thanks Nim – sdia2000 Sep 14 '12 at 10:44
  • does that class implement datasource interace ? No, it doesn't. I have given you two widely used options, just use one of them. – NimChimpsky Sep 14 '12 at 10:45