【问题标题】:Invalid bean definition with name 'dataSource' defined in class path resource [spring/database/DataSource.xml]在类路径资源 [spring/database/DataSource.xml] 中定义的名称为“dataSource”的无效 bean 定义
【发布时间】:2012-04-24 18:30:33
【问题描述】:

我正在尝试创建一个使用 Spring、Maven 和 Hibernate 访问 sql server 数据库的应用程序。当我尝试运行应用程序时出现以下错误:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [spring/database/DataSource.xml]: Could not resolve placeholder 'jdbc.driverClassName'

这是我的课程

DataSoucre.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>/properties/database.properties</value>
</property>
</bean>

<bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

</beans>

休眠.xml

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

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
  <ref bean="dataSource"/>
</property>

<property name="hibernateProperties">
   <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
     <prop key="hibernate.show_sql">true</prop>
   </props>
</property>

<property name="annotatedClasses">
<list>
    <value>com.fexco.helloworld.web.model.Customer</value>
</list>
</property>

</bean>
</beans>

还有database.properties

database.driverClassName=net.sourceforge.jtds.jdbc.Driver
database.url=jdbc:jtds:sqlserver://localhost:1433;databaseName=Customer
database.username=*****
database.password=*****

(我已经在这里屏蔽了用户名和密码),我也有一个 BeanLocation.xml,但不应该真的需要发布它。

有谁知道如何解决这个问题,这让我发疯! 谢谢

【问题讨论】:

  • 我认为问题出在您的属性文件位置。你在类路径根文件夹属性和database.properties里面有吗?

标签: java sql-server spring hibernate


【解决方案1】:

首先,您需要确保配置文件的根位置可以在类路径下访问,然后,添加属性文件的相对路径,例如。 类路径:properties/database.properties

在我的情况下,一个额外的问题是一个额外的'"'错误地出现,导致了这个错误。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    在您的 XML 中使用 ${database.driverClassName} 代替 ${jdbc.driverClassName},即在您的 database.properties 文件中使用的属性的名称。

    【讨论】:

    • 完美,成功了……我会标记它,但还没有足够的代表来做到这一点:/谢谢
    猜你喜欢
    • 2015-03-18
    • 2021-06-28
    • 2018-12-15
    • 2017-03-02
    • 2019-09-28
    • 1970-01-01
    • 2019-07-02
    • 2017-06-07
    • 2016-07-15
    相关资源
    最近更新 更多