如何在 Spring 中以编程方式解析属性占位符 [英] How to programmatically resolve property placeholder in Spring

查看:27
本文介绍了如何在 Spring 中以编程方式解析属性占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个基于 Spring 3.1.0.M1、基于注解的 Web 应用程序,但我在解决应用程序某个特定位置的属性占位符时遇到问题.

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.

这是故事.

1) 在我的 Web 应用程序上下文中(由 DispatcherServlet 加载),我有

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

mvc-config.xml:

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 个属性,其中包括:

2) Inside app.properties, there are 2 properties, among others:

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) 我的 JSP 2.1 模板中有一个 JSP 自定义标记.这个标签负责根据环境设置、应用程序版本、spring 主题选择等构建完整的资源路径.自定义标签类扩展了 spring:url 实现类,因此它可以被认为是一个普通的 url 标签,但有一些关于正确路径的额外知识.

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.

我的问题是我无法在 JSP 自定义标记实现中正确解析 ${app.resources.path}.JSP 自定义标签由 servlet 容器管理,而不是 Spring,因此不参与 DI.所以我不能只使用通常的 @Value("${app.resources.path}") 并让 Spring 自动解决它.

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.

我所拥有的只是 Web 应用程序上下文实例,因此我必须以编程方式解析我的属性.

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

到目前为止我尝试过:

ResourceTag.java:

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.

推荐答案

我认为与其关注上下文占位符的内部工作,不如简单地定义一个新的 util:properties 如下:

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

或者像这样在任何你可以做 DI 的地方:

OR like this wherever you can do DI:

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

这篇关于如何在 Spring 中以编程方式解析属性占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆