不重启服务器重新加载Spring上下文

来源:互联网 发布:php表单提交数据过滤 编辑:程序博客网 时间:2024/06/05 09:14

有时候服务器不方便重启,但是项目又需要更新,所以可以采取如下方法来重新加载Spring项目的Context

//注入ConfigurableWebApplicationContext@Resource ConfigurableWebApplicationContext wac;

在方法中调用

wac.refresh();

完整代码

import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.context.ConfigurableWebApplicationContext; @Controller@RequestMapping("/test")public class TestController {    @Resource     ConfigurableWebApplicationContext wac;    @RequestMapping("reload")    public void url(){        wac.refresh();    }}