繁体   English   中英

Spring上下文:找不到属性占位符文件

[英]Spring context:property-placeholder file not found

我正在尝试构建一个独立的基于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

有任何想法吗?

将taskExecutor.properties移至root并尝试

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

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

taskexecutor.properties重命名为taskExecutor.properties解决此问题。 哎呀。 希望这对以后和我一样愚蠢的人有所帮助;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM