-1

以下是我的目录结构

/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");

我的清单文件有以下条目。

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)

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

4

2 回答 2

0

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

我按照以下春季论坛链接进行操作,并且成功了!!!

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

于 2014-06-05T18:20:33.693 回答
0

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

因此,在创建 jar 之后,我将MANIFEST.MF文件更改为具有所需的条目并且它起作用了。

于 2014-06-05T16:10:11.823 回答