在标准 Spring 应用程序中,可以定义一个 PropertyPlaceholderConfigurer,它将加载一个或多个属性文件。然后,文件中定义的值将对应用程序的其余部分可见,无论是 XML ( "${}") 还是 Java (@Value ) 形式。
有没有办法,一旦加载了上下文,就可以从上下文本身获取这样的属性值,就像可以检索 bean 一样(ctx.getBean("bean-name") ) 一样?
我尝试了以下方法,但它不起作用:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/spring-context.xml");
ctx.refresh();
ctx.start();
ctx.getEnvironment().getProperty("key-name")); // RETURNS NULL
Run Code Online (Sandbox Code Playgroud)
谢谢
您需要访问BeanFactory:
ctx.getBeanFactory().resolveEmbeddedValue("${key-name}");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5550 次 |
| 最近记录: |