2016-05-16 27 views
1

在我的Spring MVC Web應用程序中,我在Tomcat文件夾下添加了一個database.properties文件。現在,我想如下加載從該文件中的所有屬性:在我root-contxt.xml文件,在tomcat中添加一個database.properties文件,以便在Spring MVC中存儲數據庫屬性

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value = "file:${catalina.base}/conf/database.properties"> 
    </property> 
    </bean> 

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

而在database.properties文件,我有,

database.driverClassName=my.Driver 
database.url=url 
database.username=uname 
database.password=pwd 

但我收到以下錯誤:

19:42:10.409 [localhost-startStop-1] ERROR o.s.web.context.ContextLoader - Context initialization failed 
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [spring/core/root-context.xml]: Could not resolve placeholder 'database.driverClassName' in string value "${database.driverClassName}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'database.driverClassName' in string value "${database.driverClassName}" 
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:223) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4720) [catalina.jar:8.0.18] 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154) [catalina.jar:8.0.18] 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.0.18] 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [catalina.jar:8.0.18] 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399) [catalina.jar:8.0.18] 
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_31] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_31] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_31] 
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_31] 
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'database.driverClassName' in string value "${database.driverClassName}" 
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:259) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    ... 18 common frames omitted 

有什麼辦法解決它....

回答

1

無論你是什麼路徑的驅動程序你在屬性文件中定義只是重新檢查,

+0

有沒有什麼辦法來驗證路徑? –

+0

你是路徑嘗試添加雙cotes或可能是你缺少一些syybols或特殊字符 –

相關問題