在原型bean中自动装配占位符属性

时间:2017-08-28 10:48:37

标签: java spring autowired

在我的应用程序中,我有一个定义如下的属性:

<bean id="DD6Config"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>file:${file_path}/DD6Config.properties
                </value>
            </list>
        </property>
    </bean>

我有一个原型bean(DMO_TranslationLabel),我在这里自动装配这个属性(DD6Config)。

@ANT_MapiTable(name = "TRANSLATION_LABEL")
@Component
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public class DMO_TranslationLabel
{

    @Autowired
    private Properties DD6Config;
     /*
        business logic here
     */

     if(DD6Config.containsKey("enable_translation")) 
       {
          // Business logic is property is found
       }
}

但我无法得到豆子。在if块访问时,DD6Config bean为null

但是,我可以从应用程序上下文中获取bean。

ApplicationContext  context = new ClassPathXmlApplicationContext("applicationContext.xml");
DD6Config = (Properties) context.getBeanStatic("DD6Config");

为什么我不能在原型bean中自动装配属性?当我将DMO_TranslationLabel更改为单例bean时,我能够自动装配。

1 个答案:

答案 0 :(得分:0)

我尝试了你的例子,它适用于两个范围。

您使用的是自定义ApplicationContext吗?例如,getBeanStatic不是ApplicationContext的方法。