Java无效的bean定义,名称为'dataSource',在ServletContext资源[/WEB-INF/spring-servlet.xml]中定义:

时间:2014-01-28 18:17:45

标签: java spring hibernate

我正在关注youtube教程,https://www.youtube.com/watch?v=rdYQOqxq9F0使用Spring和Hibernate添加,编辑,删除,搜索

收到此错误,

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Could not resolve placeholder 'jdbc.driverClassName'

file:jdbc.properties

jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/StudentDB
jdbc.username=root
jdbc.password=admin

文件:弹簧servlet.xml中

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

2 个答案:

答案 0 :(得分:3)

您的属性文件中似乎没有名为jdbc.driverClassName的属性。

应该是

jdbc.driverClassName=com.mysql.jdbc.Driver

答案 1 :(得分:3)

你在这些行中犯了错误

 p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}"

比较目录中的jdbc.properties文件。

示例jdbc.properties内容:

Dialect=org.hibernate.dialect.MySQLDialect// for jdbc use this. Driver=com.mysql.jdbc.Driver
Url=jdbc:mysql://localhost:3306/StudentDB
Username=root
Password=root

在ApplicationContext.xml中,它应该是这样的

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${Dialect}"
    p:url="${Url}"
    p:username="${Username}"
    p:password="${Password}" />