我的弹簧上下文文件有这个:
<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" />