2017-08-28 50 views
0

在我的應用程序有一個屬性定義如下:的Autowire佔位性質

<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 
     } 
} 

但我無法獲得該bean。在if塊中訪問時,DD6Config bean是null

但是,我可以從應用程序上下文中獲取bean。

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

爲什麼我不能自動裝配原型bean中的屬性?當我將DMO_TranslationLabel更改爲單例bean時,我可以自動裝載。

+1

嘗試爲您的媒體資源添加「@ Qualifier」。另外,請注意,您必須始終使用spring'BeanFactory'來獲取原型bean實例。 –

+0

當我搜索關於添加這一行''時,我看到了一些示例。 –

回答

0

我試過你的例子,它對兩個範圍工作正常。

您是否使用自定義ApplicationContext?例如getBeanStatic不是ApplicationContext的方法。

+0

是的,我正在使用自定義的getBeanStatic方法。 –