2012-04-12 145 views
2

我试图创建一个使用Spring,Maven和Hibernate访问sql server数据库的应用程序。当我尝试运行应用程序即时得到以下错误:在类路径资源中定义名称为'dataSource'的bean定义无效[spring/database/DataSource.xml]

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> 

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

和数据库.properties

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

(我已经阻止了用户名和密码只是为了这里),我也有一个BeanLocation.xml,但不应该真的需要发布。

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

+0

我认为,问题出在你的财产。文件的位置。你有classpath根文件夹属性和database.properties里面吗? – chalimartines 2012-04-12 12:15:10

回答

11

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

+0

完美,这是做的伎俩......我会标记它,但没有足够的代表做到这一点呢:/谢谢 – newSpringer 2012-04-12 13:30:10

0

首先,您需要确保配置文件的根目录位于classpath下,然后添加属性文件的相对路径,例如。 类路径:性能/ database.properties

在我的情况下,additinal问题是产生额外““”现在错这是导致这种错误希望这有助于

相关问题