Wicket + Spring + JPA + Hibernate:没有定义名为'dataSource'的bean

时间:2012-09-14 09:59:08

标签: spring datasource entitymanager persistence.xml

我有一个工作的Wicket + Spring + JPA + Hibernate + MySQL应用程序,分别包含以下beans.xml和persistence.xml文件。

的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>

此时我想使用JdbcTemplate来使用原始sql。对于jdbcTemplate注入,当我在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>

在部署时,最终会出现以下错误

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)
... 

如果在persistence.xml中指定了所有数据库详细信息,请如何在此处明确指定dataSource详细信息 非常感谢。

1 个答案:

答案 0 :(得分:1)

您正在引用一个名为dataSource的bean,因此您必须定义它...这是一个示例, 首先我加载一个配置文档文件:

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

然后声明DataSource,它引用配置文件设置:

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