1. 程式人生 > >Spring在web應用中使用?

Spring在web應用中使用?

/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent servletContextEvent)  { 
    //獲取Servlet的配置檔案的名稱
    ServletContext sc= servletContextEvent.getServletContext();
    String cfg=sc.getInitParameter("configLocation");
    System.out.println(cfg);
    //建立IOC容器
    ApplicationContext app= new ClassPathXmlApplicationContext(cfg);
    //把IOC容器放在ServletContext的一個屬性中
    sc.setAttribute("ApplicationContext", app);
    }