springMVC-ApplicationContext应用上下文获取

应用_上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。

在Web项目中,可以使用ServletContextListener监听Web应用的启动, 我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

web.xml



    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        AccountServlet
        dowin.web.AccountServlet
    
    
        AccountServlet
        /accountServlet
    
    
    
        dowin.listener.ContextLoaderListener
    

ContextLoaderListener

package dowin.listener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        //读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
        //将Spring的应用上下文对象存储到ServletContext中
        servletContext.setAttribute("app", app);
        System.out.println("初始化Init Listener");
    }
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}

WebApplicationContextUtils

public class WebApplicationContextUtils {
    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }
}

AccountServlet

public class AccountServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        AccountService accountService = app.getBean(AccountService.class);
        accountService.save();
    }
}

Spring提供获取应用上下文的工具

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
①在web.xml中配置ContextLoaderListener监听器 (导入spring-web坐标)
②使用WebApplicationContextUtils获得应用 上下文对象ApplicationContext

pom.xml


        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    9
                    9
                
            
        
    
    
    
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        
        
            org.springframework
            spring-web
            5.0.5.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.4
        
        
            javax.servlet
            javax.servlet-api
            3.0.1
            provided
        
        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.2.1
            provided
        

        
            junit
            junit
            4.12
            compile
        
        
        
            mysql
            mysql-connector-java
            5.1.32
        
        
        
            c3p0
            c3p0
            0.9.1.2
        
        
        
            com.alibaba
            druid
            1.1.10
        
        
            org.springframework
            spring-jdbc
            5.2.7.RELEASE
            compile
        
    

web.xml



    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        AccountServlet
        dowin.web.AccountServlet
    
    
        AccountServlet
        /accountServlet
    
    
    

        org.springframework.web.context.ContextLoaderListener
    

AccountServlet

public class AccountServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //spring自带的WebApplicationContextUtils,不是上面自定义的
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        AccountService accountService = app.getBean(AccountService.class);
        accountService.save();
    }
}

maven工程启动找不到Spring ContextLoaderListener的解决办法

报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

实际在Idea中这样配置:

File > Project Structure > Artifacts > 在右侧Output Layout右击项目名,选择Put into Output Root,见下面截图:

springMVC-ApplicationContext应用上下文获取_第1张图片

执行后,在WEB-INF在增加了lib目录,里面是项目引用的jar包,点击OK。

springMVC-ApplicationContext应用上下文获取_第2张图片

重新启动Tomcat,这次成功发布。

 

你可能感兴趣的:(java,java,spring,maven)