如何从ApplicationContext对象获取属性值? (不使用注释) [英] How do I get a property value from an ApplicationContext object? (not using an annotation)

查看:610
本文介绍了如何从ApplicationContext对象获取属性值? (不使用注释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

@Autowired private ApplicationContext ctx;

我可以使用getBean方法之一来获取bean和资源.但是,我不知道如何获取属性值.

I can get beans and resources by using one of the the getBean methods. However, I can't figure out how to get property values.

很明显,我可以创建一个具有@Value属性的新bean,如:

Obviously, I can create a new bean which has an @Value property like:

private @Value("${someProp}") String somePropValue;

在不自动装配bean的情况下,我应该在ApplicationContext对象上调用哪种方法来获取该值?

What method do I call on the ApplicationContext object to get that value without autowiring a bean?

我通常使用@Value,但是在某些情况下SPeL表达式需要动态,因此我不能只使用注释.

I usually use the @Value, but there is a situation where the SPeL expression needs to be dynamic, so I can't just use an annotation.

推荐答案

在SPeL表达式需要动态的情况下,请手动获取属性值:

In the case where SPeL expression needs to be dynamic, get the property value manually:

somePropValue = ctx.getEnvironment().getProperty("someProp");

这篇关于如何从ApplicationContext对象获取属性值? (不使用注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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