spring 细节

1、spring中ApplicationContext对象在项目启动的时候只创建一次,因为ApplicationContext在加载applicationContext.xml文件的时候会创建配置的所有对象,所以只能创建一次。

第一步:

  导入一个spring架包,spring-web-4.2.4.RELEASE.jar

第二步:

  在web.xml中配置监听

<!-- 可以让spring容器随项目的启动而创建,随项目的关闭而销毁 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定加载spring配置文件的位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

第三步:

  创建对象

//获得spring容器=>从Application域获得即可
//1 获得servletContext对象
ServletContext sc = ServletActionContext.getServletContext();
 //2.从Sc中获得ac容器
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
原文地址:https://www.cnblogs.com/ms-grf/p/7389762.html