현재 스프링 3.1.0.M1을 기반으로하는 웹 응용 프로그램에서 주석을 기반으로 작업하며 응용 프로그램의 특정 위치에서 속성 자리 표시 자 해결과 관련된 문제가 있습니다.스프링에서 프로퍼티 자리 표시자를 프로그래밍 방식으로 해결하는 방법
다음은 이야기입니다. (내가
MVC-config.xml에 있습니다) DispatcherServlet에 의해로드 내 웹 응용 프로그램의 맥락에서
1) :
<!-- 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를,이 속성은 다른 사람의 사이가 :
app.properties :
# Properties provided (filtered) by Maven itself
app.version: 0.1-SNAPSHOT
...
# Static resources mapping
app.resources.path: resources/${app.version}
3) 내 JS에서 JSP 사용자 정의 태그를 P 2.1 템플릿. 이 태그는 환경 설정, 앱 버전, 봄 테마 선택 등에 따라 전체 리소스 경로를 생성합니다. 맞춤 태그 클래스는 봄 : URL 구현 클래스를 확장하므로 일반적인 URL 태그로 간주 될 수 있지만 적절한 경로에 대한 추가 지식이 있습니다.
내 문제는 내 JSP 사용자 정의 태그 구현에서 $ {app.resources.path}를 올바르게 해석 할 수 없다는 것입니다. JSP 커스텀 태그는 Spring이 아닌 서블릿 컨테이너에 의해 관리되므로 DI에 참여하지 않아야한다. 그래서 나는 보통의 @Value ("$ {app.resources.path}")를 사용할 수없고 Spring에 의해 자동적으로 해결된다.
나는 웹 응용 프로그램 컨텍스트 인스턴스가 있으므로 프로그래밍 방식으로 내 속성을 해결해야합니다.
는 지금까지 시도 :
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");
그래서 지금은 좀 그와 함께 붙어있어. 내 자리 표시자를 해결할 수있는 능력이 문맥에 어딘가에 있다는 것을 알고 있습니다. 올바른 방법을 모릅니다.
확인할 수있는 도움이나 아이디어가 있으면 감사하겠습니다. 이 같은 특성을 :
<util:properties id="appProperties" location="classpath:app.properties" />
및 코드에서, 다음과 같이 사용 :
감사합니다. Ritesh. 나는이 솔루션을 사용할 수 있다고 생각한다. –
이 답변을 수락 됨으로 표시했지만, 순수 프로그래밍 방식 솔루션을 찾는 것은 흥미 롭습니다. –
@Max Alexejev PropertyPlaceholderConfigurer를 확장하고 processProperties 메서드에 두 번째 인수로 전달 된 해결 된 속성을 캡처 할 수도 있습니다. 그러나 context 대신 context-bean을 사용해야한다. – Ritesh