0

I use properties file in spring framework

root-context.xml

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

java code

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

when Using url call(@RequestMapping(value="/recommendbooks" , method=RequestMethod.GET, produces="application/json;charset=UTF-8")).. this work!

but, i use method call,

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

when 'this point ' method call, changing controller, and don't read properties file field..


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

It doesn't read IndexBasePath

4

1 に答える 1

0

の新しいインスタンスを作成しているコードではIndexManageController、Spring はこのインスタンスを認識しないため、処理されません。

public void executeInternal(JobExecutionContext arg0) throws JobExecutionException {

    IndexManageController indexManage = new IndexManageController();

新しいインスタンスを作成する代わりに、依存関係を注入してIndexManageController、Spring によって構築および管理される事前構成済みのインスタンスを使用するようにします。(そして、そのクラスの新しいインスタンスを構築する行を削除します)。

public class MyJob {

    @Autowired
    private IndexManageController indexManage;

}

構成もプロパティを2回ロードしています

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

どちらも config.properties ファイルをロードします。config を property-placeholder 要素に接続するだけです。

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

2 回のロードを節約し、別の Bean を節約します。

于 2013-11-05T07:24:40.750 に答える