【问题标题】:Spring context:property-placeholder file not foundSpring 上下文:找不到属性占位符文件
【发布时间】:2013-01-24 13:20:26
【问题描述】:

我正在尝试构建一个独立的基于 Spring 的 Java 应用程序。

我正在使用 Maven 程序集插件来构建具有依赖项的 JAR。 JAR 的结构最终看起来像这样(只是一个子集):

com
META-INF
org
properties
 - taskexecutor.properties
spring
 - properties.xml
.
.
.

清单如下所示:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: authorname
Build-Jdk: 1.6.0_30
Main-Class: com.company.utilities.task.TaskLauncher

当应用程序运行时,应通过包含 properties.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:property-placeholder location="classpath:/properties/taskExecutor.properties"/>

</beans> 

它在 Eclipse 中运行良好,但是当我运行 JAR 时出现此错误:

Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileN
otFoundException: class path resource [properties/taskExecutor.properties] cannot be opened because it does not exist
        at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.company.utilities.task.TaskLauncher.main(TaskLauncher.java:28)
Caused by: java.io.FileNotFoundException: class path resource [properties/taskExecutor.properties] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
        at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
        at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
        at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
        ... 6 more

有什么想法吗?

【问题讨论】:

    标签: java spring maven jar maven-assembly-plugin


    【解决方案1】:

    将 taskExecutor.properties 移至 root 并尝试

    <context:property-placeholder location="taskExecutor.properties"/>
    

    【讨论】:

      【解决方案2】:

      通过命令行运行似乎区分大小写,但加载这些资源时 Eclipse 不区分。

      taskexecutor.properties 重命名为taskExecutor.properties 解决了这个问题。哎呀。希望这可以帮助将来像我一样愚蠢的人;-)

      【讨论】:

        猜你喜欢
        • 2013-05-29
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        • 2013-12-06
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多