以编程方式在Spring 3.1中加载属性 [英] Loading properties in Spring 3.1 programmatically

查看:136
本文介绍了以编程方式在Spring 3.1中加载属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式创建AnnotationConfigApplicationContext。我得到一个配置类列表和一个属性文件列表,以便在Spring XML文件中使用它。

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.

使用该文件,我可以使用XmlBeanDefinitionReader和加载所有@Configuration定义。但是,我无法加载属性。

Using that file, I am able to use XmlBeanDefinitionReader and load all @Configuration definitions fine. But, I am not able to load properties.

这是我正在做的加载属性..

This is what I am doing to load properties..

PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx);
for (String propFile : propertyFiles) {
    propReader.loadBeanDefinitions(new ClassPathResource(propFile));
}

代码只运行这个没有任何问题,但一旦我拨打
ctx.refresh() - 它抛出异常

Code just runs through this without any issues, but once I call ctx.refresh() -- it throws an exception

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.

推荐答案

我不确定你为什么要手动加载属性,但是AnnotationConfigApplicationContext的Spring标准是

I am not sure why you are loading properties manually, but Spring standard for AnnotationConfigApplicationContext is

@Configuration
@PropertySource({"/props1.properties", "/props2.properties"})
public class Test {
...

对于编程加载,使用PropertySourcesPlaceholderConfigurer而不是PropertiesBeanDefinitionReader,这个例子工作正常

as for programatic loading, use PropertySourcesPlaceholderConfigurer instead of PropertiesBeanDefinitionReader, this example works OK

@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);
    }
}

打印

val1

这篇关于以编程方式在Spring 3.1中加载属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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