如何以编程方式解析Spring中的属性占位符

Max*_*jev 37 spring jsp dependency-injection jsp-tags custom-tag

我目前正在开发基于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
    "/>
Run Code Online (Sandbox Code Playgroud)

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}
Run Code Online (Sandbox Code Playgroud)

3)我在JSP 2.1模板中有一个JSP自定义标记.此标记负责完整的资源路径构建,具体取决于环境设置,应用程序版本,弹簧主题选择等.自定义标记类扩展spring:url实现类,因此它可能被视为通常的url标记,但具有关于正确路径的一些额外知识.

我的问题是我无法在JSP自定义标记实现中正确解析$ {app.resources.path}.JSP自定义标记由servlet容器管理,而不是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");
Run Code Online (Sandbox Code Playgroud)

所以现在我有点坚持.我知道解析我的占位符的能力是在上下文中的某个地方,我只是不知道正确的方法.
任何帮助或想要检查的想法都非常感谢.

Eds*_*Eds 37

从Spring 3.0.3开始,有一个EmbeddedValueResolverAware,它的工作方式与另一个使用appContext.getBeanFactory().resolveEmbeddedValue("${prop}")call的帖子相同.

解决问题:

  1. 让您的类实现EmbeddedValueResolverAware接口,您将为您注入解析器

  2. 然后,您将能够检索代码片段中演示的属性:

    String propertyValue = resolver.resolveStringValue("${your.property.name}");
    
    Run Code Online (Sandbox Code Playgroud)

然后,您的bean不需要依赖ApplicationContext来检索您需要的属性.

  • 这是解决问题的一种非常简单的方法.对于任何未来的读者,只需将接口EmbeddedValueResolverAware添加到spring-managed类,在该类中,您希望能够以编程方式解析属性,实现setter方法,并使用@Eds编写的代码来解析. (4认同)

thi*_*ibr 23

从版本3.0开始,Spring在beanFactory中保留了一个String解析器列表.你可以像这样使用它:

String value = appContext.getBeanFactory().resolveEmbeddedValue("${prop}");
Run Code Online (Sandbox Code Playgroud)

javadoc声明这个方法用于解析嵌入值(例如注释属性),所以我们可能会绕过它的用法,但它可以工作.


Rit*_*esh 15

我认为,而不是专注于上下文占位符的内部工作,你可以简单地定义一个新的util:这样的属性:

<util:properties id="appProperties" location="classpath:app.properties" />
Run Code Online (Sandbox Code Playgroud)

并在您的代码中,使用它像这样:

Properties props = appContext.getBean("appProperties", Properties.class);
Run Code Online (Sandbox Code Playgroud)

或者像这样,无论你在哪里做DI:

@Value("#{appProperties['app.resources.path']}")
Run Code Online (Sandbox Code Playgroud)


Joh*_*erg 5

一种选择是添加一个PropertySource(这里MapPropertySource举例说明一个内存配置)ConfigurableEnvironment并要求它为你解析属性.

public class Foo {

    @Autowired
    private ConfigurableEnvironment env;

    @PostConstruct
    public void setup() {
        env.getPropertySources()
           .addFirst(new MapPropertySource("my-propertysource", 
               ImmutableMap.<String, Object>of("your.property.name", "the value")));
        env.resolvePlaceholders("your.property.name");
    }
}
Run Code Online (Sandbox Code Playgroud)

可选择注释Foo该类,@Configuration以享受编程配置的强大功能XML.