我的春天上下文文件包含:
<context:property-placeholder location="classpath:web.properties" />
我引用了键/值,如:
<property name="username" value="${dataSource.username}"/>
我的文件布局如下:
>/www/site/app.war
>/www/site/configs/web.properties
我使用以下方式运行我的应用程序:
>java -cp "/www/site/configs/*.*" -jar app.war
但后来我得到了这个例外:
java.io.FileNotFoundException: class path resource [web.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
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:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:661)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
即使我将文件添加到我的类路径中,它仍然找不到原因?
更新
所以我的配置文件在这里:
/www/site/configs/web.properties
/www/site/configs/log4j.properties
我试过了:
>/www/site/java -cp ".:app.war:/www/site/configs/*.*" com.abc.server.MyServer
和
>/www/site/java -cp ".:app.war:/www/site/configs/web.properties:/www/site/configs/log4j.properties" com.abc.server.MyServer
甚至:
/ www / site / java -cp“。:app.war”com.abc.server.MyServer
我得到同样的错误:
2013-04-25 01:19:28.210:INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2013-04-25 01:19:28.294:INFO:oejw.WebInfConfiguration:Extract jar:file:/www/site/app.war!/ to /www/site/work/app
2013-04-25 01:19:32.814:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/www/site/app/},file:/www/site/app.war
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2013-04-25 01:19:33.585:INFO:/:Initializing Spring FrameworkServlet 'app'
2013-04-25 01:19:35.750:WARN:/:unavailable
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/web-context.xml]: Could not resolve placeholder 'dataSource.url' in string value [${dataSource.url}]
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
我的web.properties有:
dataSource.url=jdbc:mysql://localhost/appdb
dataSource.username=root
dataSource.password=123
我的web-context.xml文件再次出现:
..
<context:property-placeholder location="classpath*:/web.properties" />
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${dataSource.url}"/>
<property name="username" value="${dataSource.username}"/>
<property name="password" value="${dataSource.password}"/>
..
我还尝试了以下变体:
<context:property-placeholder location="classpath:web.properties" />
<context:property-placeholder location="classpath*:web.properties" />
<context:property-placeholder location="classpath*:/web.properties" />
答案 0 :(得分:4)
我也遇到过类似的问题。我不能说为什么,但我发现-cp参数在与-jar一起拖拽时不会表现。
你可以使用,
修改强>
根据讨论并经历:http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html
正确的语法应该是:
在Windows中:
java -cp "app.war;/www/site/configs/" <Main Class file name>
在Linux中:
java -cp "app.war:/www/site/configs/" <Main Class file name>
答案 1 :(得分:2)
通过CLASSPATH访问的项目根本不一定是文件。它们可能仍在JAR或WAR文件中。它们应该通过Class.getResource()和朋友作为资源,进行访问。
CLASSPATH中的条目也不是文件。根据包结构,CLASSPATH是一个或多个目录或JAR文件,其中可以找到资源。