現在、Spring 3.1.0.M1に基づくWebアプリケーション、アノテーションに基づいて作業していますが、アプリケーションの特定の場所でプロパティプレースホルダーを解決する際に問題が発生します。
これが物語です。
1)私のWebアプリケーションコンテキスト(DispatcherServletによってロードされる)では、
mvc-config.xml:
<!-- Handles HTTP GET requests for /resources/version/** -->
<resources mapping="/${app.resources.path}/**" location="/static/" cache-period="31556926"/>
...
<!-- Web properties -->
<context:property-placeholder location="
classpath:app.properties
"/>
2)app.properties内には、特に2つのプロパティがあります。
app.properties:
# Properties provided (filtered) by Maven itself
app.version: 0.1-SNAPSHOT
...
# Static resources mapping
app.resources.path: resources/${app.version}
3)JSP2.1テンプレートにJSPカスタムタグがあります。このタグは、環境設定、アプリのバージョン、Springテーマの選択などに応じて、完全なリソースパスの構築を担当します。カスタムタグクラスはspring:url実装クラスを拡張するため、通常のurlタグと見なすことができますが、適切なパスに関する追加の知識があります。
私の問題は、JSPカスタムタグの実装で${app.resources.path}を正しく解決できないことです。JSPカスタムタグはSpringではなくサーブレットコンテナによって管理されるため、DIには参加しません。したがって、通常の@Value( "$ {app.resources.path}")を使用して、Springによって自動的に解決されるようにすることはできません。
私が持っているのはWebアプリケーションコンテキストインスタンスだけなので、プログラムでプロパティを解決する必要があります。
これまでに試しました:
ResourceTag.java:
// returns null
PropertyResolver resolver = getRequestContext().getWebApplicationContext().getBean(PropertyResolver.class);
resolver.getProperty("app.resources.path");
// returns null, its the same web context instance (as expected)
PropertyResolver resolver2 = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).getBean(PropertyResolver.class);
resolver2.getProperty("app.resources.path");
// throws NPE, resolver3 is null as StringValueResolver is not bound
StringValueResolver resolver3 = getRequestContext().getWebApplicationContext().getBean(StringValueResolver.class);
resolver3.resolveStringValue("app.resources.path");
// null, since context: property-placeholder does not register itself as PropertySource
Environment env = getRequestContext().getWebApplicationContext().getEnvironment();
env.getProperty("app.resources.path");
だから今、私はちょっとそれに固執しています。プレースホルダーを解決する機能はコンテキストのどこかにあることを知っていますが、それを行う正しい方法がわかりません。
チェックするヘルプやアイデアは大歓迎です。