从runnable jar访问外部Spring配置文件

时间:2014-06-04 21:01:08

标签: java spring classpath executable-jar component-scan

以下是我的目录结构

/app/infra/myjar/bin --> contains my runnable jar
/app/infra/myjar/config --> contains all external config files which I want to read from code which is inside jar.

以下是我的代码来读取外部弹簧配置文件

ApplicationContext basicConfig =  new ClassPathXmlApplicationContext("classpath:/spring-bean-mapping.xml");

我的MANIFEST文件有以下条目。

Class-Path:
.
../config/

此外,我已将.bashrc中的CLASSPATH变量设置为指向/ app / infra / myjar / config文件夹

当我尝试使用

运行jar时
java -jar myjar.jar

我遇到了异常

Loading Spring Bean Mapping file
log4j:WARN No appenders could be found for logger        (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-bean-mapping.xml]; nested exception is      java.io.FileNotFoundException: class path resource [spring-bean-mapping.xml] cannot be opened because it does not exist
    at  org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.vzw.esper.alerts.consumer.EsperMessagesConsumer.main(EsperMessagesConsumer.java:47)
Caused by: java.io.FileNotFoundException: class path resource [spring-bean-mapping.xml]  cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 13 more
Exception in thread "main" java.lang.NullPointerException
    at com.vzw.esper.alerts.consumer.EsperMessagesConsumer.main(EsperMessagesConsumer.java:86)

我怎样才能理解这里出了什么问题?

2 个答案:

答案 0 :(得分:0)

幸运的是,我能够解决上述问题。问题是当我通过Eclipse创建一个可执行jar时,它覆盖了我的MANIFEST.MF类路径条目。

所以在jar创建之后我更改了MANIFEST.MF文件以获得所需的条目并且它有效。

答案 1 :(得分:0)

我能够解决第二个问题,即我遇到弹簧组件扫描的问题。

我按照春季论坛链接进行了操作!!!

http://forum.spring.io/forum/spring-projects/container/89556-component-scanning-works-in-ide-but-not-in-an-executable-jar-file