簡體   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