部署后,Spring 不再找到 .Jar 之外的 JDBC.Properties
最近出现了将数据库设置从 Spring applicationContext.xml 外包出去的需求。因此,我们希望在构建的 jar 之外有一个 jdbc.properties 文件,以便用户可以轻松更改 jdbc.settings。到目前为止,在 Eclipse 工作区中一切工作正常,但是当我使用 ant 构建发行版时,applicationContext.xml 就不再找到 jdbc.properties。
applicationContext.xml 如下所示:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
在 eclipse 中,我使用了以下类路径,因此 jdbc.properties(放置在 eclipse 项目文件夹“config”中)将会找到:
类路径文件:
<classpath>
...
<classpathentry kind="src" path="config"/>
...
</classpath>
直到这里一切正常。现在我用 Ant 构建项目。将创建project.jar,并在其旁边创建一个单独的config 文件夹,其中包含jdbc.properties 文件。在 Manifest.mf 中有一个条目:
config/jdbc.properties
但是当我现在启动服务器时,找不到 jdbc.properties。我注意到,当我将 Manifest.mf 条目更改为:
config/.
它又可以正常工作了。但我不明白为什么这是不同的。谁能向我解释这种行为?或者甚至知道我做错了什么?显然我必须以某种方式更改 Ant 文件,所以他只是在清单文件中添加了 config 文件夹,而不仅仅是 jdbc.properties 文件本身?
先感谢您!
lately a requirement came up to outsource the database settings out of Spring applicationContext.xml. Therefore we wanted to have a jdbc.properties file outside the builded jars, so the user can easily change the jdbc.settings. So far all worked fine within the eclipse workspace, but as soon as I build the distribution with ant, the applicationContext.xml doesnt find the jdbc.properties anymore.
The applicationContext.xml looks like the following:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Within eclipse I used following classpath, so the jdbc.properties (which is placed in an eclipse project folder "config") gonna get found:
classpath file:
<classpath>
...
<classpathentry kind="src" path="config"/>
...
</classpath>
Until here all works fine. Now I build the project with Ant. The project.jar gets created, and a seperate config folder right next to it as well with the jdbc.properties file in it. Within the Manifest.mf there is an entry:
config/jdbc.properties
But when I now start the server, the jdbc.properties are not found. I noticed, when I change the Manifest.mf entry to:
config/.
it works fine again. But I dont understand why this is different. Can anyone explain me this behaviour? Or maybe even knows what I am doing wrong? Obviously I have to change the Ant file somehow, so he just adds the config folder in the manifest file, and not only the jdbc.properties-file itself?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类路径保存 jar 文件的位置以及保存类文件和其他资源的目录。您应该在类路径中指定保存属性文件的目录的路径,而不是属性文件本身的路径。也就是说,您的清单条目应该是“config”。
The classpath holds the locations of jar files and directories holding class files and other resources. You should specify in your classpath the path to the directory holding the properties file, rather than the path to the properties file itself. That is, your manifest entry should be 'config'.
解决方案是在 Ant-buildscript 中定义一个类路径元素。
不幸的是,我仍然不知道,为什么直接引用该文件也不能解决这个问题。
Solution was to define an classpathelement within the Ant-buildscript.
Unfortunatly I still dont know, why refering directly to the file didnt solve this issue as well.