Spring的配置文件那么多,再加上现在项目中有过多 的配置文件,总是要不断的修改,导致多次频繁重启应用服务器。于是,就想来点自动化(应该是手动化)。
这个东西从网上找了好几次,总是没有合意的。
但是在浏览Spring源代码时,发现了一个AbstractRefreshableWebApplicationContext,里面有个Refreshable,于是就看到了那点曙光。不断的抓住WebApplicationContextUtils这根稻草,使劲挖掘WebApplicationContext对象,只要保证我的对象是XmlWebApplicationContext就好。追究XmlWebApplicationContext的祖宗们,呵呵,终于发现了AbstractRefreshableConfigApplicationContext类,这个类的父类就应该是我要找的东西了:AbstractApplicationContext。
这个类中包含了两个我找了很长时间的方法:
public void destroy();
public void refresh() throws BeansException, IllegalStateException;
看看就知道这两个方法的意思了。问题也就应该可以解决了吧!
写了一个jsp页面用于访问,里面的代码实现:
<%
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
FastClass fClass = FastClass.create(springContext.getClass());
FastMethod destroyMethod = fClass.getMethod("destroy",new Class[]{});
destroyMethod.invoke(springContext,null);
FastMethod refreshMethod = fClass.getMethod("refresh",new Class[]{});
refreshMethod.invoke(springContext,null);
out.println("refresh already!");
%>
这个代码里面使用了,cglib里面的FastClass和FastMethod。