java 以编程方式在 Spring 3.1 中加载属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14167332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading properties in Spring 3.1 programmatically
提问by endless
I am trying to create a AnnotationConfigApplicationContext programmatically. I am getting a list of Configuration classes and a list of property files to go with it in a Spring XML file.
我正在尝试以编程方式创建 AnnotationConfigApplicationContext。我在 Spring XML 文件中得到一个配置类列表和一个属性文件列表。
Using that file, I am able to use XmlBeanDefinitionReader and load all @Configuration definitions fine. But, I am not able to load properties.
使用该文件,我可以使用 XmlBeanDefinitionReader 并加载所有 @Configuration 定义。但是,我无法加载属性。
This is what I am doing to load properties..
这就是我正在做的加载属性..
PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx);
for (String propFile : propertyFiles) {
propReader.loadBeanDefinitions(new ClassPathResource(propFile));
}
Code just runs through this without any issues, but once I call ctx.refresh() -- it throws an exception
代码只是通过这个没有任何问题,但是一旦我调用 ctx.refresh() - 它会引发异常
Caused by: java.lang.IllegalStateException: No bean class specified on bean definition
at org.springframework.beans.factory.support.AbstractBeanDefinition.getBeanClass(AbstractBeanDefinition.java:381)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:54)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
All the classes are available on the classpath, if I just don't load the above properties programmatically application just comes up fine (Because I am using other way to load properties).
所有的类都在类路径上可用,如果我只是不以编程方式加载上述属性,应用程序就会正常运行(因为我使用其他方式加载属性)。
Not sure, what I am doing wrong here. Any ideas? Thanks.
不确定,我在这里做错了什么。有任何想法吗?谢谢。
回答by Evgeniy Dorofeev
I am not sure why you are loading properties manually, but Spring standard for AnnotationConfigApplicationContext is
我不确定您为什么要手动加载属性,但是 AnnotationConfigApplicationContext 的 Spring 标准是
@Configuration
@PropertySource({"/props1.properties", "/props2.properties"})
public class Test {
...
as for programatic loading, use PropertySourcesPlaceholderConfigurer instead of PropertiesBeanDefinitionReader, this example works OK
至于程序加载,使用 PropertySourcesPlaceholderConfigurer 而不是 PropertiesBeanDefinitionReader,这个例子工作正常
@Configuration
public class Test {
@Value("${prop1}") //props1.properties contains prop1=val1
String prop1;
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(Test.class);
PropertySourcesPlaceholderConfigurer pph = new PropertySourcesPlaceholderConfigurer();
pph.setLocation(new ClassPathResource("/props1.properties"));
ctx.addBeanFactoryPostProcessor(pph);
ctx.refresh();
Test test = ctx.getBean(Test.class);
System.out.println(test.prop1);
}
}
prints
印刷
val1