之前的博客搭建spring mvc应用的时候,我们使用spring中的ContextLoaderListener和DispatcherServlet分别加载了不同的spring配置文件。spring mvc程序里会有两个WebApplicationContext,一个是parent,从applicationContext.xml里加载的,一个是child,从servlet-context.xml里加载的。 两者是继承关系,child WebApplicationContext 可以通过getParent()函数获取到root WebApplicationContext。
还是之前的代码和配置,我们修改下FirstController.java来验证下父子关系:
package net.aty.springmvc;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import net.aty.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
@Controller
public class FirstController {
@Autowired
private MyService se