2013-11-05 95 views
0

我使用屬性在Spring框架文件屬性不是從屬性中找到文件中使用@Value

根的context.xml

<context:property-placeholder location="classpath:config.properties" /> 
<util:properties id="config" location="classpath:config.properties" /> 

Java代碼

@Value("#{config[ebookUseYN]}") 
String EBOOKUSEYN; 

使用URL調用(當@RequestMapping(value="/recommendbooks" , method=RequestMethod.GET, produces="application/json;charset=UTF-8"))..這項工作!

但是,我使用的方法調用,

public void executeInternal(JobExecutionContext arg0) throws JobExecutionException { 

IndexManageController indexManage = new IndexManageController(); 
CommonSearchDTO commonSearchDTO = new CommonSearchDTO(); 

try {   
     if("Y".equals(EBOOKUSEYN)){ 
      indexManage.deleteLuceneDocEbook(); 
      indexManage.initialBatchEbook(null, commonSearchDTO); 
     } 
     indexManage.deleteLuceneDoc(); <= this point 
     indexManage.deleteLuceneDocFacet(); 

     indexManage.initialBatch(null, commonSearchDTO); 


    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

當 '這點' 方法調用,改變控制器和不讀屬性文件場..


@Value("#{config[IndexBasePath]}") 
    String IndexBasePath; 

@RequestMapping(value="/deleteLuceneDoc" , method=RequestMethod.GET, produces="application/json;charset=UTF-8") 
    public @ResponseBody ResultCodeMessageDTO deleteLuceneDoc() 
      throws Exception 
{ 

long startTime = System.currentTimeMillis(); 

ResultCodeMessageDTO result = new ResultCodeMessageDTO(); 
System.out.println(IndexBasePath); 
} 

它不讀IndexBasePath

+0

你確定你的屬性文件包含一個'IndexBasePath'條目並且它寫在你的代碼中嗎?請注意,條目名稱應該按原樣書寫,因爲Spring將解析這個區分大小寫。 –

+0

當然。 'IndexBasePath = D:\\ BTBF_Index \\'條目是在屬性文件..和其他情況下使用此條目..但這種情況下不工作.. – HoHoHo

回答

0

在你的代碼中,你正在創建一個新的實例IndexManageController,Spring不知道這個實例,因此它永遠不會被處理。

public void executeInternal(JobExecutionContext arg0) throws JobExecutionException { 

    IndexManageController indexManage = new IndexManageController(); 

而不是創建一個新的實例注入了IndexManageController的依賴,以便它使用由Spring構建和管理的預配置實例。 (並刪除構建該類的新實例的那一行)。

public class MyJob { 

    @Autowired 
    private IndexManageController indexManage; 

} 

你的配置也加載性能的兩倍

<context:property-placeholder location="classpath:config.properties" /> 
<util:properties id="config" location="classpath:config.properties" /> 

兩個加載config.properties文件。只需將配置連接到屬性佔位符元素。

<context:property-placeholder properties-ref="config"/> 
<util:properties id="config" location="classpath:config.properties" /> 

保存你加載兩次,並保存你另一個bean。

+0

如何注入IndexManageController的依賴項.. ?????彈簧太硬...... – HoHoHo

+0

添加字段,放入'@ Inject'(或'@ Autowired'或'@ Resource')並完成。沒有什麼難的。 –

相關問題