Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

jsp - How to programmatically resolve property placeholder in Spring

I currently work on a web application based on Spring 3.1.0.M1, annotations based, and I have a problem with resolving property placeholders in one specific place of my application.

Here is the story.

1) In my web application context (loaded by DispatcherServlet), i have

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) Inside app.properties, there are 2 properties, among others:

app.properties:

# Properties provided (filtered) by Maven itself
app.version: 0.1-SNAPSHOT
...

# Static resources mapping
app.resources.path: resources/${app.version}

3) I have a JSP custom tag in my JSP 2.1 templates. This tag is responsible for full resource path construction depending on environment settings, app version, spring theme selection etc. Custom tag class extends spring:url implementation class, so it may be considered a usual url tag but with some additional knowledge about proper path.

My problem is that I cannot get ${app.resources.path} correctly resolved in my JSP custom tag implementation. JSP custom tags are managed by servlet container, not Spring, and therefore dont participate in DI. So I cannot just use usual @Value("${app.resources.path}") and get it resolved by Spring automatically.

All I have there is the web application context instance, so I have to resolve my property programmatically.

So far I tried:

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");

So now I'm kinda stuck with that. I know that the ability to resolve my placeholder is somewhere in the context, I just don't know the correct way to do it.
Any help or ideas to check are highly appreciated.

question from:https://stackoverflow.com/questions/5172392/how-to-programmatically-resolve-property-placeholder-in-spring

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think rather than focusing on inner working of context place holder, you can simply define a new util:properties like this:

<util:properties id="appProperties" location="classpath:app.properties" />

and in your code, use it like this:

Properties props = appContext.getBean("appProperties", Properties.class);

OR like this wherever you can do DI:

@Value("#{appProperties['app.resources.path']}")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...