为 Spring PropertyPlaceholderConfigurer bean 设置类路径
Setting the classpath for a Spring PropertyPlaceholderConfigurer bean
我一直在查看与 Spring PropertyPlaceholderConfigurer
beans 和相关类路径相关的问题,但到目前为止无法解决我的问题。我正在对一个名为 myApp
的旧程序进行一些更改,该程序 运行 是一个 Jar,并且有一个名为 myApp.properties' file, which lives in a directory called 'config' within my application directory. In the
applicationContext.xml' 的外部属性文件,用于“myApp”我定义了一个 bean 来读取这个外部属性文件:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:myApp.properties</value>
</list>
</property>
此应用程序在 Netbeans 中构建和 运行 时工作正常,因为我包括 config
文件夹以及构建和 运行s 的所有 JAR 依赖项。但是当我尝试使用 java -jar myApp.jar
从命令行 运行 它时,我收到以下错误:
08/01/2016 15:37:18.562 | ERROR | Unable to start the application. | org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [myApp.properties] cannot be opened because it does not exist
我知道我需要正确指定类路径,以便 Spring 知道在哪里可以找到属性文件,但我不确定具体如何操作,所以任何指针都将不胜感激.. .
试试,
java -classpath ".;\config*" -jar myApp.jar
最终对我有用的是:
java -cp "myApp.jar;lib/*;config" com.myCompany.myApp.Main
(是的,有main()方法的class也叫Main。不,这个程序不是我写的:))
我的 classpath 指定包含主要 class 的 JAR,目录 lib
包含 运行 此应用程序所需的所有其他 JAR(使用 ' *' 字符以避免必须写出该目录中每个 JAR 的名称),以及包含各种配置文件的 'config' 目录。我还发现我可以在 属性-configurer 定义中指定 myApp.properties
而无需任何限定符,这样我就可以将该文件保存在与 'myApp.jar' 相同的目录中而不会出现问题。
我一直在查看与 Spring PropertyPlaceholderConfigurer
beans 和相关类路径相关的问题,但到目前为止无法解决我的问题。我正在对一个名为 myApp
的旧程序进行一些更改,该程序 运行 是一个 Jar,并且有一个名为 myApp.properties' file, which lives in a directory called 'config' within my application directory. In the
applicationContext.xml' 的外部属性文件,用于“myApp”我定义了一个 bean 来读取这个外部属性文件:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:myApp.properties</value>
</list>
</property>
此应用程序在 Netbeans 中构建和 运行 时工作正常,因为我包括 config
文件夹以及构建和 运行s 的所有 JAR 依赖项。但是当我尝试使用 java -jar myApp.jar
从命令行 运行 它时,我收到以下错误:
08/01/2016 15:37:18.562 | ERROR | Unable to start the application. | org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [myApp.properties] cannot be opened because it does not exist
我知道我需要正确指定类路径,以便 Spring 知道在哪里可以找到属性文件,但我不确定具体如何操作,所以任何指针都将不胜感激.. .
试试,
java -classpath ".;\config*" -jar myApp.jar
最终对我有用的是:
java -cp "myApp.jar;lib/*;config" com.myCompany.myApp.Main
(是的,有main()方法的class也叫Main。不,这个程序不是我写的:))
我的 classpath 指定包含主要 class 的 JAR,目录 lib
包含 运行 此应用程序所需的所有其他 JAR(使用 ' *' 字符以避免必须写出该目录中每个 JAR 的名称),以及包含各种配置文件的 'config' 目录。我还发现我可以在 属性-configurer 定义中指定 myApp.properties
而无需任何限定符,这样我就可以将该文件保存在与 'myApp.jar' 相同的目录中而不会出现问题。