我的 context.xml 中有以下内容:
<bean id="myBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${specific.dir}/file1.properties</value>
<value>file:${specific.dir}/file2.properties</value>
<value>file:${specific.dir}/file3.properties</value>
<value>file:${specific.dir}/file4.properties</value>
</list>
</property>
</bean>
我正在通过静态 spring 应用程序上下文在 POJO 中检索此 bean,并通过调试器,这似乎确实有效。
我是否可以检索列表中的四个值以及此 POJO 中的属性名称?
我的 staticSpringApplicationContext 如下:
public class StaticSpringApplicationContext implements ApplicationContextAware {
private static ApplicationContext CONTEXT;
public void setApplicationContext(ApplicationContext context) throws BeansException {
CONTEXT = context;
}
public static Object getBean(String beanName) {
return CONTEXT.getBean(beanName);
}
}
在我的 POJO 中我有以下内容:
StaticSpringApplicationContext.getBean("myBean");
非常感谢任何帮助或指导。我在 Spring 方面遇到的麻烦比我愿意承认的还要多。
最佳答案
您可以使用 spring @Value 注解,如下所示:
@Component
public MyClass {
@Value("${some.prop.name1}")
private String myProp1;
}
文件1.属性:
some.prop.name1=value1
希望对您有所帮助。祝你好运。
关于java - 从 Spring bean 中以字符串形式检索属性/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17869349/