Spring项目里面用到阿里数据库连接池:druid,按照正常配置,出现了:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean def
inition with name 'dataSource' defined in class path resource [applicationContex
t.xml]: Could not resolve placeholder 'druid.url' in string value "${druid.url}"
; nested exception is java.lang.IllegalArgumentException: Could not resolve plac
eholder 'druid.url' in string value "${druid.url}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport
.doProcessProperties(PlaceholderConfigurerSupport.java:211)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigure
r.processProperties(PropertyPlaceholderConfigurer.java:222)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.p
ostProcessBeanFactory(PropertyResourceConfigurer.java:86)
at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate
.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
at org.springframework.context.support.AbstractApplicationContext.invoke
BeanFactoryPostProcessors(AbstractApplicationContext.java:606)
配置文件:druid.properties:
druid.url=jdbc:mysql://127.0.0.1:3307/test
druid.username=root
druid.password=123456
applicationContext.xml:
<!-- 基本属性 url、user、password -->
<property name="url" value="${druid.url}" />
<property name="username" value="${druid.username}" />
<property name="password" value="${druid.password}" />
理论上,应该不会报出那异常,在开发环境上正常,部署到其他win tomcat环境下却爆出来,自己尝试了N种方法,最终才找到问题的缘由。
最后原因也是没想到的。
原因如下:
竟然是文件保存的编码问题!真的是醉了。。。
打开查看编码竟然是UTF-8 BOM,但是我本地查看的是UTF-8.这两个到底是什么原因????
百度知道上有人回答:
“utf-8+bom比utf-8多了三个字节前缀:0xEF0xBB0xBF,有这三个字节前缀的文本或字符串,程序可以自动判断它为utf-8格式,
并按照utf-8格式来解析文本或字符串,否则,一个文本或者字符串在未知编码的情况下,需要按照字符编码规范去一个个验证。”
我自己个人验证的方法:
在druid.properties上,第一行换行。尝试启动。=》正常
修改文件编码为UTF-8,没换行。尝试启动。=》正常
终于真相大白了,厉害了Word的哥!!!
修改文件编码参考图:editplus软件