私の春のコンテキストファイルにはこれがあります:
<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" />