0

我在spring框架中使用属性文件

根上下文.xml

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

爪哇代码

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

当使用 url call( @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();
  }
}

当 'this point' 方法调用时,更改控制器,并且不读取属性文件字段..


@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

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;

}

您的配置还加载了两次属性

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

于 2013-11-05T07:24:40.750 回答