0

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

豆类.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>

持久性.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 中指定所有数据库详细信息时,如何在此处明确指定数据源详细信息非常感谢。

4

1 回答 1

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"/>
于 2012-09-14T10:09:23.877 回答